解决org.hibernate.MappingNotFoundException与数据库连接异常问题
解决Hibernate MappingNotFound、XML解析错误及数据库连接问题
看起来你遇到了几个关联的问题,咱们一步步来拆解解决:
1. 优先修复XML解析错误(class元素内容不完整)
日志里明确提示:The content of element type "class" is incomplete,这是因为你的hibernate.hbm.xml里的<class>标签只定义了实体类和表的映射,但没有添加任何字段/属性的映射,完全不符合Hibernate的DTD规范要求。
你的当前映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Login.Login" table="login1"/> </hibernate-mapping>
需要补充实体类的属性映射,比如假设你的Login类有id(主键)、username、password三个核心字段,修改后的映射文件应该是这样:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="Login.Login" table="login1"> <!-- 主键映射,对应数据库表的主键字段 --> <id name="id" column="id"> <generator class="native"/> <!-- 使用数据库自增主键策略 --> </id> <!-- 普通属性映射,要和实体类属性名、数据库字段名完全对应 --> <property name="username" column="username" type="string"/> <property name="password" column="password" type="string"/> </class> </hibernate-mapping>
2. 解决MappingNotFoundException: resource: hibernate.hbm.xml not found
这个错误通常是因为映射文件不在Hibernate能识别的classpath路径下:
- 如果你是Web项目,把
hibernate.hbm.xml放在项目的src根目录下(编译后会自动复制到WEB-INF/classes目录,属于classpath范围); - 如果是Maven项目,直接放在
src/main/resources目录下; - 检查文件名大小写,Hibernate对路径和文件名是严格区分大小写的,确保配置里的
<mapping resource="hibernate.hbm.xml"/>和实际文件名完全一致。
3. 修复数据库连接失败问题
看你的hibernate.cfg.xml,有两个关键问题导致数据库连不上:
- 密码属性被注释掉了:如果你的MySQL root用户设置了密码,必须取消注释并填写真实密码:
<property name="hibernate.connection.password">你的MySQL密码</property> - (可选适配高版本MySQL)如果你的MySQL是8.0及以上版本,需要调整驱动类和URL:
- 驱动类改为
com.mysql.cj.jdbc.Driver - URL添加时区参数:
jdbc:mysql://localhost:3306/jsfformdb?useSSL=false&serverTimezone=UTC
- 驱动类改为
- 另外,
connection.pool_size可以取消注释,设置合理的值(比如5),提升连接池的使用效率。
修改后的hibernate.cfg.xml示例:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- MySQL 8.0+ 替换为 com.mysql.cj.jdbc.Driver --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsfformdb?useSSL=false&serverTimezone=UTC</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">你的真实密码</property> <property name="connection.pool_size">5</property> <property name="hbm2ddl.auto">update</property> <mapping resource="hibernate.hbm.xml"/> </session-factory> </hibernate-configuration>
4. 优化登录代码(避免资源泄漏)
你的登录代码里,SessionFactory的创建方式在Hibernate 4+之后已经过时,而且没有正确关闭Session和SessionFactory,容易导致资源泄漏,建议调整为:
public boolean login() { SessionFactory sessionFactory = null; Session session = null; try{ // Hibernate 4+ 推荐的SessionFactory创建方式 StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // 自动加载classpath下的hibernate.cfg.xml .build(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); session = sessionFactory.openSession(); session.beginTransaction(); Query query = session.createQuery("from Login where username=:username and password=:password"); query.setString("username", username); query.setString("password", password); List<?> list = query.list(); System.out.println("list size" + list.size()); session.getTransaction().commit(); // 正常执行时提交事务 return list.size() == 1; } catch(Exception e) { // 出错时回滚事务 if(session != null && session.getTransaction().isActive()){ session.getTransaction().rollback(); } e.printStackTrace(); // 打印完整异常栈,更方便排查问题 } finally { // 确保资源被关闭 if(session != null) session.close(); if(sessionFactory != null) sessionFactory.close(); } return false; }
内容的提问来源于stack exchange,提问作者kannan
相关产品推荐
相关产品推荐

