Hibernate映射异常[试过以前的解决方案]

时间:2015-07-18 13:15:39

标签: java hibernate maven

我正在尝试运行基于maven的Hibernate Hello World项目。我已经完成了每一步,但它给出了Hibernate Mapping Exception:Unknown Entity。我已经在Hibernate.cfg.xml中声明了我的Entity类的映射。

这是我的配置文件代码。

<?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;
}

我的模特课     package 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在映射已经完成时抛出异常。我差不多一年前做过这次。它的工作方式相同。需要帮助。

现在情况有点扭曲。我通过改变hibernate-core的maven依赖来绕过异常。我得到了这种依赖的异常。这是maven网站上的最新消息。

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

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

6 个答案:

答案 0 :(得分:2)

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

ijson.common.JSONError: Additional data

其次,不要使用Configuration with StandardServiceRegistryBuilder,配置被认为已弃用, 而是按照hibernate 5文档中的说明进行引导,以供参考,转到下面的链接,

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

或者你可以修改你的代码,如下所示,试试我希望它能运作

public class Book implements Serializable{
}

答案 1 :(得分:1)

您的图书未实现Serializable。 试试这个。

public class Book implements Serializable{
}

答案 2 :(得分:1)

不要将配置与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();

有关详细信息,请查看documentation

答案 3 :(得分:0)

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

-Madhu。

答案 4 :(得分:0)

-Your 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;
        }
    }

答案 5 :(得分:0)

显然你应该添加,但不会产生影响:

  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>