Java org.hibernate.MappingException:未知实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4001795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.MappingException: Unknown entity
提问by Stefan Kendall
I'm trying to work through Beginning Hibernate 2nd edition, and I'm stuck trying to put together the simple working example with HSQLDB.
我正在尝试完成 Beginning Hibernate 2nd edition,但我一直在尝试将简单的工作示例与 HSQLDB 放在一起。
When I run ant populateMessages
, I get
当我跑步时ant populateMessages
,我得到
[java] org.hibernate.MappingException: Unknown entity: sample.entity.Message
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:747)
...
Here's what I've got:
这是我所拥有的:
Message.java
消息.java
package sample.entity;
import org.hibernate.annotations.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Message
{
private String messageText;
private Integer id;
public Message( String messageText )
{
this.messageText = messageText;
}
public Message()
{
}
public String getMessageText()
{
return messageText;
}
public void setMessageText(String messageText)
{
this.messageText = messageText;
}
@Id
@GeneratedValue
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
}
PopulateMessages.java
填充消息.java
package sample;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import sample.entity.Message;
import java.util.Date;
public class PopulateMessages
{
public static void main(String[] args)
{
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Message m1 = new Message("Hibernated a messages on " + new Date());
session.save(m1);
session.getTransaction().commit();
session.close();
}
}
build.properties
构建属性
# Path to the hibernate install directory
hibernate.home=C:/hibernate/hibernate-3.5.6
# Path to the hibernate-tools install directory
hibernate.tools.home=C:/hibernate/hibernate-tools
# Path to hibernate-tools.jar relative to hibernate.tools.home
hibernate.tools.path=/plugins/org.hibernate.eclipse_3.3.1.v201006011046R-H111-GA/lib/tools
# Path to hibernate-tools hibernate libraries relative to hibernate.tools.home
hibernate.tools.lib.path=/plugins/org.hibernate.eclipse_3.3.1.v201006011046R-H111-GA/lib/hibernate
# Path to the SLF4J implementation JAR for the logging framework to use
slf4j.implementation.jar=lib/slf4j-simple-1.6.1.jar
# Path to the HSQL DB install directory
hsql.home=C:/hsqldb
hibernate.cfg.xml
休眠文件.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:hsqldb:file:testdb;shutdown=true
</property>
<property name="hibernate.connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.dialect">
org.hibernate.dialect.HSQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<!-- "Import" the mapping resources here -->
<mapping class="sample.entity.Message"/>
</session-factory>
</hibernate-configuration>
build.xml
构建文件
<project name="sample">
<property file="build.properties"/>
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="sql" location="sql"/>
<property name="hibernate.tools"
value="${hibernate.tools.home}${hibernate.tools.path}"/>
<path id="classpath.base">
<pathelement location="${src}"/>
<pathelement location="${bin}"/>
<pathelement location="${hibernate.home}/hibernate3.jar"/>
<pathelement location="${slf4j.implementation.jar}"/>
<fileset dir="${hibernate.home}/lib" includes="**/*.jar"/>
<pathelement location="${hsql.home}/lib/hsqldb.jar"/>
<fileset dir="./lib" includes="**/*.jar"/>
</path>
<path id="classpath.tools">
<path refid="classpath.base"/>
<pathelement
location="${hibernate.tools.home}/${hibernate.tools.lib.path}/commons-logging-1.0.4.jar"/>
<pathelement
location="${hibernate.tools}/freemarker.jar"/>
<pathelement
location="${hibernate.tools}/hibernate-tools.jar"/>
</path>
<taskdef name="htools"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="classpath.tools"/>
<target name="exportDDL" depends="compile">
<mkdir dir="${sql}"/>
<htools destdir="${sql}">
<classpath refid="classpath.tools"/>
<annotationconfiguration
configurationfile="${src}/hibernate.cfg.xml"/>
<hbm2ddl drop="true" outputfilename="sample.sql"/>
</htools>
</target>
<target name="compile">
<javac srcdir="${src}" destdir="${bin}" classpathref="classpath.base"/>
</target>
<target name="populateMessages" depends="compile">
<java classname="sample.PopulateMessages" classpathref="classpath.base"/>
</target>
<target name="listMessages" depends="compile">
<java classname="sample.ListMessages" classpathref="classpath.base"/>
</target>
采纳答案by Pascal Thivent
You entity is not correctly annotated, you mustuse the @javax.persistence.Entity
annotation. You can use the Hibernate extension @org.hibernate.annotations.Entity
to go beyond what JPA has to offer but the Hibernate annotation is not a replacement, it's a complement.
您的实体未正确注释,您必须使用@javax.persistence.Entity
注释。您可以使用 Hibernate 扩展@org.hibernate.annotations.Entity
来超越 JPA 所提供的功能,但 Hibernate 注释不是替代品,而是一种补充。
So change your code into:
所以把你的代码改成:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Message {
...
}
References
参考
- Hibernate Annotations Reference Guide
- Hibernate 注释参考指南
回答by Bozho
You should call .addAnnotatedClass(Message.class)
on your AnnotationConfiguration
.
你应该调用.addAnnotatedClass(Message.class)
你的AnnotationConfiguration
.
If you want your entities to be auto-discovered, use EntityManager
(JPA)
如果您希望自动发现您的实体,请使用EntityManager
(JPA)
(参考)
Update: it appears you have listed the class in hibernate.cfg.xml. So auto-discovery is not necessary. Btw, try javax.persistence.Entity
更新:看来您已经在 hibernate.cfg.xml 中列出了该类。所以不需要自动发现。顺便说一句,试试javax.persistence.Entity
回答by M.J.
you should add all the entity files in the .addAnnotatedClass(Class) method, if the class needs to be auto discovered.
如果需要自动发现类,您应该在 .addAnnotatedClass(Class) 方法中添加所有实体文件。
use this link, it might help..
使用此链接,它可能会有所帮助..
http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/cfg/AnnotationConfiguration.html
http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/cfg/AnnotationConfiguration.html
回答by Satya Johnny
In hibernate.cfg.xml , please put following code
在 hibernate.cfg.xml 中,请输入以下代码
<mapping class="class/bo name"/>
回答by Sanjay Ingole
I encountered the same problem when I switched to AnnotationSessionFactoryBean
. I was using entity.hbm.xml
before.
当我切换到AnnotationSessionFactoryBean
. 我entity.hbm.xml
以前用过。
I found that My class was missing following annotation which resolved the issue in my case:
我发现我的班级在注释之后丢失了,这解决了我的问题:
@Entity
@Table(name = "MyTestEntity")
@XmlRootElement
回答by Saravana
In case if you get this exception in SpringBoot
application even though the entities are annotated with Entity
annotation, it might be due to the spring not aware of where to scan for entities
如果您在SpringBoot
应用程序中遇到此异常,即使实体用注释进行了Entity
注释,这可能是由于 spring 不知道在哪里扫描实体
To explicitly specify the package, add below
要明确指定包,请在下面添加
@SpringBootApplication
@EntityScan({"model.package.name"})
public class SpringBootApp {...}
note: If you model classes resides in the same or sub packages of SpringBootApplication
annotated class, no need to explicitly declare the EntityScan
, by default it will scan
注意:如果您的模型类驻留在SpringBootApplication
注解类的相同或子包中,则无需显式声明EntityScan
,默认情况下它会扫描
回答by Akanksha
Use import javax.persistence.Entity;Instead of import org.hibernate.annotations.Entity;
使用import javax.persistence.Entity; 而不是import org.hibernate.annotations.Entity;
回答by JavaDev
Use below line of code in the case of Spring Boot Application Add in Spring Boot Main Class @EntityScan(basePackageClasses=YourClassName.class)
在 Spring Boot Application Add in Spring Boot Main Class @EntityScan(basePackageClasses=YourClassName.class)的情况下使用下面的代码行
回答by JavaDev
use below line of code in the case of spring boot applications.
在 Spring Boot 应用程序的情况下使用下面的代码行。
@EntityScan(basePackageClasses=YourClassName.class)
@EntityScan(basePackageClasses=YourClassName.class)
回答by Santosh Reddy
My issue was resolved After adding
sessionFactory.setPackagesToScan(
new String[] { "com.springhibernate.model" });
Tested this Functionality in spring boot latest version 2.1.2.
添加
sessionFactory.setPackagesToScan( new String[] { "com.springhibernate.model" });后,我的问题得到解决。在 spring boot 最新版本 2.1.2 中测试了这个功能。
Full Method:
完整方法:
@Bean( name="sessionFactoryConfig")
public LocalSessionFactoryBean sessionFactoryConfig() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSourceConfig());
sessionFactory.setPackagesToScan(
new String[] { "com.springhibernate.model" });
sessionFactory.setHibernateProperties(hibernatePropertiesConfig());
return sessionFactory;
}