我正在尝试入门Hibernate 2nd Edition,而我一直试图将简单的工作示例与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>
您的实体注释不正确, 必须
使用@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 {
...
}
问题内容: 我真的很想了解我的代码发生了什么。 我有一个独立的应用程序,它使用spring和Hibernate作为JPA,我正在尝试使用单个主类运行测试 我的主班 我的应用程序上下文是: 我的用户模型是: 我的DAO是: 这是我运行程序时出现的我的stacktrace错误: 问题答案: 您必须在会话工厂配置中列出您的类。如果使用,则可以自动发现实体。 为了在hibernate和spring中使用注
此实体由Spring控制器管理,方法如下: 服务类别为: 而DAO是这样的:
我在应用程序中使用了Spring3和Hibernate4。在运行时,我得到的异常值低于异常值。
问题内容: 我写了一些代码将数据插入SQL数据库。实际上,我正在尝试使用Hibernate学习Struts2。但是,不幸的是,我在提交表格后遇到了问题。 我找不到此错误消息的原因。我的try&catch块引发如下错误: Pojo(): 还具有: 问题答案: 如果未映射实体,则应检查hibernate配置。Hibernate是ORM框架,用于将pojo(实体)映射到数据库架构对象。您没有配置或hib
查询: 错误: 在我的网上搜索中,几乎所有论坛主题的答案都是一致的:“把@实体放在类声明之前”,但是,这对我来说不起作用。
我有一个测试车, 而applicatin.xml是 但它报告异常 java.lang.IllegalArgumentException:未知实体:com.chinalbs.entity.conductor(位于org.hibernate.ejb.abstractentyManagerImpl.persist(abstractentyManagerImpl.java:842)(位于sun.refle