我试图通过初休眠第二版的工作,和我被困试图把连同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;
}
}
populate messages.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>
您实体没有被正确注解,你必须使用@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 {
...
}
可以通过添加下面注解上的.java @EntityScan应用使实体扫描(basePackageClasses = YourEntityClassName.class)
或者你可以在你的会话工厂设置packageToScan。 sessionFactory.setPackagesToScan(“com.all.entity”);
在hibernate.cfg.xml文件中,请把下面的代码
@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;
}
你应该叫.addAnnotatedClass(Message.class)
您AnnotationConfiguration
。
如果你希望你的实体是自动发现,使用EntityManager
(JPA)
(Qazxswpoi)
更新:看来你已经在hibernate.cfg.xml列出的类。因此,自动发现是没有必要的。顺便说一句,尽量Reference
你应该添加在.addAnnotatedClass(Class)方法的所有实体文件,如果类需要被自动发现。
使用此链接,它可能会帮助..
javax.persistence.Entity
我遇到了同样的问题,当我切换到http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/cfg/AnnotationConfiguration.html。我用AnnotationSessionFactoryBean
之前。
我发现,我的班级是缺少以下标注其解决了我的情况的问题:
entity.hbm.xml
在情况下,如果你得到这个例外@Entity
@Table(name = "MyTestEntity")
@XmlRootElement
应用,即使实体与SpringBoot
注解,这可能是由于春不知道哪里来扫描实体
要明确指定的包,下面添加
Entity
注意:如果您的模型类驻留在@SpringBootApplication
@EntityScan({"model.package.name"})
public class SpringBootApp {...}
注解类的相同或子包,无需显式声明SpringBootApplication
,默认情况下它会扫描
使用进口javax.persistence.Entity;取而代之的进口org.hibernate.annotations.Entity;
下面使用的春季引导应用程序的情况下的代码行添加春启动主类@EntityScan(basePackageClasses = YourClassName.class)
在弹簧引导应用的情况下使用下面的代码行。
@EntityScan(basePackageClasses = YourClassName.class)
我国问题解决后加入 sessionFactory.setPackagesToScan(新的String [] { “com.springhibernate.model”});在春季启动最新版本2.1.2测试了这个功能。
完整的方法:
EntityScan