我正在尝试使用Beginning Hibernate第二版,而我却试图将简单的工作示例与HSQLDB放在一起。
当我运行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)
...
这就是我所拥有的:
Message.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
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中
<!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>
答案 0 :(得分:34)
您的实体未正确注释,必须使用@javax.persistence.Entity
注释。您可以使用Hibernate扩展@org.hibernate.annotations.Entity
超出JPA提供的范围,但Hibernate注释不是替代,它是一个补充。
所以将代码更改为:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Message {
...
}
答案 1 :(得分:12)
您应该在.addAnnotatedClass(Message.class)
上致电AnnotationConfiguration
。
如果您希望自动发现您的实体,请使用EntityManager
(JPA)
更新:您似乎已在hibernate.cfg.xml中列出了该类。因此不需要自动发现。顺便说一下,试试javax.persistence.Entity
答案 2 :(得分:0)
如果需要自动发现类,则应在.addAnnotatedClass(Class)方法中添加所有实体文件。
使用此链接,它可能有帮助..
http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/cfg/AnnotationConfiguration.html
答案 3 :(得分:0)
切换到AnnotationSessionFactoryBean
时遇到了同样的问题。我之前使用的是entity.hbm.xml
。
我发现我的课程在注释后遗失了,这解决了我的问题:
@Entity
@Table(name = "MyTestEntity")
@XmlRootElement
答案 4 :(得分:0)
如果您在SpringBoot
应用程序中获得此异常,即使实体使用Entity
注释进行注释,也可能是因为spring不知道在哪里扫描实体
要明确指定包裹,请在下面添加
@SpringBootApplication
@EntityScan({"model.package.name"})
public class SpringBootApp {...}
注意:如果模型类位于SpringBootApplication
带注释类的相同或子包中,则无需显式声明EntityScan
,默认情况下它将扫描
答案 5 :(得分:0)
使用 导入javax.persistence.Entity; 而不是 导入org.hibernate.annotations.Entity;
答案 6 :(得分:0)
在Spring Boot Application的情况下使用以下代码行 添加Spring Boot主类 @EntityScan(basePackageClasses = YourClassName.class)
答案 7 :(得分:0)
在弹簧启动应用程序的情况下使用下面的代码行。
<强> @EntityScan(basePackageClasses = YourClassName.class)强>
答案 8 :(得分:0)
添加后我的问题已解决
sessionFactory.setPackagesToScan(
新的String [] {“ com.springhibernate.model”});
在Spring Boot最新版本2.1.2中测试了此功能。
完整方法:
@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;
}
答案 9 :(得分:0)
检查是否在hibernate.cfg.xml中定义了实体。
答案 10 :(得分:-1)
您可以通过在Application .java上添加以下注释来启用实体扫描 @EntityScan(basePackageClasses = YourEntityClassName.class)
或者您可以在会话工厂中设置packageToScan。 sessionFactory.setPackagesToScan(“ com.all.entity”);
答案 11 :(得分:-2)
在hibernate.cfg.xml中,请输入以下代码
<mapping class="class/bo name"/>