【问题标题】:Hibernate Mapping Exception [Tried previous solutions]Hibernate Mapping Exception [尝试过以前的解决方案]
【发布时间】:2015-07-18 13:15:38
【问题描述】:

我正在尝试运行基于 Maven 的 Hibernate Hello World 项目。我已经完成了每一步,但它给出了休眠映射异常:未知实体。我已经在 Hibernate.cfg.xml 中声明了我的实体类的映射。

这是我的配置文件代码。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>


    <!-- Database connection settings -->

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup
         if table isn't present hibernate will create the table -->
    <property name="hbm2ddl.auto">create</property>

    <property name="hibernate.current_session_context_class">thread</property>
    <mapping class="com.openlibrary.model.Book"/>


</session-factory>
</hibernate-configuration>

这是会话工厂代码

public static SessionFactory getSessionFactory() {

    if (sessionFactory == null) {

        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    }
    return sessionFactory;
}

还有我的模特班 包 com.openlibrary.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue
    int bookID;
    String bookName;

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
}

这里是个例外

Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.openlibrary.model.Book
at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:787)
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2637)
at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2575)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2562)
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
at com.openlibrary.dao.BookDAO.getBookById(BookDAO.java:43)
at com.openlibrary.test.Testing.main(Testing.java:22)

我知道以前有人问过这样的问题但是我无法理解为什么当映射已经完成时休眠会抛出异常。我上次做了差不多一年前。它以同样的方式工作。在这里需要帮助。

现在场景有点扭曲。我通过更改我对休眠核心的 Maven 依赖项绕过了异常。我遇到了这种依赖的异常。这是 Maven 网站上的最新版本。

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.0.CR2</version>
 </dependency>

我将版本更改为 4.3.7.Final。然后也不例外。我不知道为什么它会抛出异常。也许有人可以解释。我会很感激的。

【问题讨论】:

  • 文件应该命名为hibernate.cfg.xml(区分大小写,h而不是H)?你确定你真的使用了你的配置文件(可能你的类路径中有另一个配置文件?或者你有没有将你的实体类打包到应用程序中?
  • 你能发布你得到的异常吗?
  • @diufanman 抱歉,我刚刚在问题中输入错误。它实际上是 hibernate.cfg.xml 和我的项目中的相同。是的,我很确定 w 配置文件,并且我已经正确打包了实体类。
  • @Rdx 我正在添加有问题的异常。现在剧情有点转折。我通过更改 hibernate-core 的 maven 依赖项绕过了异常。我遇到了这种依赖的异常。休眠核心和版本 5.0.0.CR2。这是 Maven 网站上的最新版本。 [在最后的问题中添加了确切的代码]但是当我将版本更改为 4.3.7.Final 时。然后也不例外。我不知道为什么它会抛出异常。也许你可以解释一下。我会很感激的。 :)

标签: java hibernate maven


【解决方案1】:

首先在你的实体类中实现Serializable接口,

public class Book implements Serializable{
}

其次,不要将 Configuration 与 StandardServiceRegistryBuilder 一起使用,Configuration 被认为已弃用, 而是按照 hibernate 5 文档中提到的那样进行引导,以供参考,请转到下面的链接,

http://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#bootstrap-native-sessionfactory

或者您可以修改您的代码如下尝试我希望它会工作

if (sessionFactory == null) {

        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        configuration.addClass( Book.class ).addResource( "com/openlibrary/model/Book.hbm.xml" );
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(serviceRegistry.build());

    }

【讨论】:

    【解决方案2】:

    您的书未实现可序列化。 试试这个。

    public class Book implements Serializable{
    }
    

    【讨论】:

      【解决方案3】:

      不要将 Configuration 与 StandardServiceRegistryBuilder 一起使用,Configuration 被认为已弃用,而是按照 hibernate 5 文档中的说明进行引导,我遇到了同样的问题,这解决了它。

      StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
      .configure( "org/hibernate/example/MyCfg.xml" )
      .build();
      
      Metadata metadata = new MetadataSources( standardRegistry )
      .addAnnotatedClass( MyEntity.class )
      .addAnnotatedClassName( "org.hibernate.example.Customer" )
      .addResource( "org/hibernate/example/Order.hbm.xml" )
      .addResource( "org/hibernate/example/Product.orm.xml" )
      .getMetadataBuilder()
      .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
      .build();
      
      SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
      .applyBeanManager( getBeanManagerFromSomewhere() )
      .build();
      

      更多详情,请查看documentation

      【讨论】:

        【解决方案4】:

        我相信,在最新版本的 hibernate 中,他们已经删除了对配置文件中 &lt;mapping&gt; 标记的支持。相反,您可以使用配置类中定义的addAnnotatedClass(&lt;Your domain class&gt;) 方法。即 configuration.addAnnotatedClass(Book.class);在这种情况下。

        -马杜。

        【讨论】:

          【解决方案5】:

          -您的 Book Model 类没有实现 Serializable 接口。

          -试试下面的代码。

          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.Id;
          
              @Entity
              public class Book implements Serializable {
                  @Id
                  @GeneratedValue
                  int bookID;
                  String bookName;
          
                  public int getBookID() {
                      return bookID;
                  }
          
                  public void setBookID(int bookID) {
                      this.bookID = bookID;
                  }
          
                  public String getBookName() {
                      return bookName;
                  }
          
                  public void setBookName(String bookName) {
                      this.bookName = bookName;
                  }
              }
          

          【讨论】:

            【解决方案6】:

            显然你应该添加,但没有影响:

              public class Book implements Serializable{
               }          
            

            但是当我将版本更改为 4.3.7.Final 时。然后也不例外。

                <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-core</artifactId>
                  <version>4.3.7.Final</version>
               </dependency>
            

            【讨论】:

              猜你喜欢
              • 2012-09-07
              • 2014-09-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多