java HibernateMap异常[已尝试以前的解决方案]

jm2pwxwz  于 2023-05-15  发布在  Java
关注(0)|答案(7)|浏览(157)

我试图运行一个基于maven的Hibernate Hello World项目。我已经做了正确的每一步,但它给出了HibernateMap异常:未知实体。我已经在Hibernate.cfg.xml中声明了我的Entity类的Map。
下面是我的配置文件代码。

<?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)

我知道以前有人问过这样的问题,但我不明白为什么Hibernate在Map完成后会抛出异常。我上次是一年前做的。它是以同样的方式工作的。这里需要帮助。
现在情况有了一点变化。我通过改变我对hibernate-core的maven依赖绕过了这个异常。我得到了这个依赖关系的异常。这是maven网站上的最新消息。

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

我把版本改成了4.3.7.Final。那就没有例外。我不知道为什么会抛出异常。也许有人能解释。我会很感激的

yrdbyhpb

yrdbyhpb1#

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

public class Book implements Serializable{
}

其次,不要在StandardServiceRegistryBuilder中使用Configuration,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());

    }
mu0hgdu0

mu0hgdu02#

你的书没有实现序列化。试试这个。

public class Book implements Serializable{
}
uyto3xhc

uyto3xhc3#

不要使用配置与StandardServiceRegistryBuilder,配置被认为是不推荐的,而是使引导中提到的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();

有关详细信息,请查看文档

noj0wjuj

noj0wjuj4#

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

  • 玛杜
bjg7j2ky

bjg7j2ky5#

  • 您的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;
        }
    }
iqjalb3h

iqjalb3h6#

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

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>
k10s72fa

k10s72fa7#

对我来说,这是一个事实,在某个时候,hibernate开始使用jakarta.persistence.* 的注解,而不是java.persistence.*。

相关问题