【发布时间】: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 时。然后也不例外。我不知道为什么它会抛出异常。也许你可以解释一下。我会很感激的。 :)