org.hibernate.MappingException: Unknown entity:

本文针对初学者在使用Hibernate过程中常见的“org.hibernate.MappingException: Unknown entity”错误进行了解析,并提供了详细的排查步骤及解决方案。

许多初学者在学习Hibernate的时候,总会遇到这样的问题。org.hibernate.MappingException: Unknown entity: ******

今天我在调试程序的时候也遇到了此问题,最终终于解决,下面我的一些总结下。

一句话:“因为你的映射文件(*.hbm.xml) 没有被映射”

问题出现的原因,有几下几点:

1,检查你的映射文件的名字是否和你的pojo(*.java)的名字是否相同。

2.映射文件的名字是*.hbm.xml而不是*.xml

3.你是否加载了你的映射文件。

      加载的方法有两种

   (1)在你的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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

              …………

              …………

         <!-- 用于记载映射文件Student.hbm.xml-->
     <mapping resource="com/Students.hbm.xml"/>
     </session-factory>

</hibernate-configuration>

    (2)在你的测试代码的加载映射文件

           Configuration cfg = new Configuration();
           cfg.configure();
           cfg.addClass(*.class);    这里的*.class是你的映射文件的名字*.hbm.class中的*。。。

      但是你要注意,用第二种方法加载的时候,你的*.hbm.class文件必须位于classpath下面。

 

 

在使用 Hibernate 进行实体映射时,出现 `org.hibernate.MappingException: Unknown entity` 异常通常是由于 Hibernate 无法识别指定的实体类。以下是常见的解决方法和注意事项: ### 1. 检查实体类的注解引入路径 确保使用了正确的 `@Entity` 注解包路径。Hibernate 推荐使用 JPA 标准注解,因此应引入 `javax.persistence.Entity`,而不是 `org.hibernate.annotations.Entity`。错误的引入会导致 Hibernate 无法识别实体类[^1]。 ```java import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "test_person") public class Person implements Serializable { // 实体类内容 } ``` ### 2. 注册实体类或映射文件 Hibernate 需要知道哪些类是实体类,或者需要加载哪些映射文件(`.hbm.xml`)。可以通过以下方式注册实体类或映射文件: - **使用配置文件注册实体类**:在 `hibernate.cfg.xml` 文件中添加 `<mapping>` 标签来注册实体类。 ```xml <mapping class="com.example.entity.Person"/> ``` - **注册 `.hbm.xml` 映射文件**:如果使用 XML 文件进行映射,确保在 `hibernate.cfg.xml` 中正确注册了映射文件[^4]。 ```xml <mapping resource="com/example/entity/Person.hbm.xml"/> ``` ### 3. 使用 `MetadataSources` 注册实体类 如果通过编程方式创建 `SessionFactory`,可以在构建 `MetadataSources` 时显式注册实体类。 ```java ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); MetadataSources metadataSources = new MetadataSources(serviceRegistry); metadataSources.addAnnotatedClass(Person.class); // 注册实体类 SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); ``` ### 4. 检查实体类的可序列化性 如果实体类实现了 `Serializable` 接口,确保该类确实实现了该接口,并且 `Serializable` 的引入路径正确。 ```java import java.io.Serializable; @Entity @Table(name = "test_person") public class Person implements Serializable { // 实体类内容 } ``` ### 5. 检查实体类的包扫描配置 如果使用 Spring Boot 或其他框架,Hibernate 会自动扫描实体类所在的包。确保在配置文件中正确设置了实体类的包路径。 ```yaml spring: jpa: hibernate: use-new-id-generator-mappings: false properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect packagesToScan: com.example.entity ``` ### 6. 确保依赖版本兼容性 检查项目的依赖版本是否兼容,尤其是 Hibernate Core 和 JPA 的版本。版本不兼容可能导致 Hibernate 无法识别实体类。 ### 7. 检查实体类的命名和表名 确保实体类的名称与数据库表名一致,或者通过 `@Table(name = "table_name")` 明确指定表名。表名和列名的大小写也需要匹配,尤其是在区分大小写的数据库中。 ### 示例代码:完整的实体类定义 ```java import javax.persistence.Entity; import javax.persistence.Table; import java.io.Serializable; @Entity @Table(name = "test_person") public class Person implements Serializable { // 实体类内容 } ``` ### 示例代码:Hibernate 配置文件 `hibernate.cfg.xml` ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接配置 --> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <!-- Hibernate 配置 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <!-- 注册实体类 --> <mapping class="com.example.entity.Person"/> </session-factory> </hibernate-configuration> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值