我是hibernate的新手。使用它创建一个小应用程序时,出现以下异常:
线程“主”中的异常java.lang.IllegalArgumentException:未知实体:
model.Students在org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:223)在controller.Main.main(Main.java:50)
有人可以帮我吗?
实体类如下:
Other details:
NetBeans Version: 6.7.1
Hibernate : 3.2.5
实体学生
package model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Students implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToOne(cascade=CascadeType.ALL)
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
另一个实体类
package model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String city;
private String zip;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
DAO文件
package controller; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import model.Address;
import model.Students;
import org.hibernate.HibernateException;
public class Main {
public static void main(String arr[])
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToOne2PU");
EntityManager em = emf.createEntityManager();
EntityTransaction tr= em.getTransaction();
try{
tr.begin();
Address add1 = new Address();
add1.setCity("pune");
add1.setZip("09");
Address add2 = new Address();
add2.setCity("mumbai");
add2.setZip("12");
Students s1 = new Students();
s1.setName("abc");
s1.setAddress(add1);
Students s2 = new Students();
s2.setName("xyz");
s2.setAddress(add2);
em.persist(s1);
em.persist(s2);
tr.commit();
emf.close();
}
catch(HibernateException e){
e.printStackTrace();
}
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="OneToOnePU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>students</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-tables"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.connection.username" value="app"/>
<property name="hibernate.connection.password" value="app"/>
<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
</properties>
</persistence-unit>
</persistence>
hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!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.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<mapping class="model.Students"/>
<mapping class="model.Address"/>
</session-factory>
</hibernate-configuration>
取决于项目结构,但可能是通过在persistence-unit元素下直接在persistence.xml中添加以下内容。
<class>model.Students</class>
<class>model.Address</class>
完全像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="OneToOnePU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>students</jta-data-source>
<class>model.Students</class>
<class>model.Address</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-tables"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.connection.username" value="app"/>
<property name="hibernate.connection.password" value="app"/>
<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
</properties>
</persistence-unit>
</persistence>
顺便说一句,为什么要在persistence.xml和hibernate.cfg.xml中都配置诸如 hibernate.dialect之类的 属性?
我有一个Hibernate实体,,它工作正常: 我想确定此对象何时更改并在我的应用程序中更新一些。我能想到的最简单的方法是扩展并覆盖setter: 这在我的应用程序中工作得很好,但是当我开始持久化实体时,Hibernate抱怨它不知道什么是< code>ExtendedEvent。 我可以看出扩展Hibernate实体是一个不小的问题,尤其是当您开始添加字段时——但是我所希望的是Hibernate
我有一个测试车, 而applicatin.xml是 但它报告异常 java.lang.IllegalArgumentException:未知实体:com.chinalbs.entity.conductor(位于org.hibernate.ejb.abstractentyManagerImpl.persist(abstractentyManagerImpl.java:842)(位于sun.refle
问题内容: 到目前为止,给我带来了很多麻烦,所以我想摆脱它。尽管spring框架文档清楚地说明了应该做的事情,但实际上 并没有摘要列表。 所以我一直坚持删除并得到错误 -在名称为的中找不到带有请求的映射 对于所有应该由控制器类解决的Url(在这种情况下:)。有什么建议可以让我了解更多信息吗?我非常想知道到底由代表什么标签。 问题答案: 你可以用来自定义定义的每个bean 。现在,javadocs详
问题内容: 到现在为止,给我造成了很多麻烦,所以我想摆脱它。尽管spring框架文档清楚地说明了应该做的事情,但实际上 并没有摘要列表。 所以我坚持删除并现在得到错误 WARN osweb.servlet.PageNotFound-在DispatcherServlet中,名称为’workoutsensor’的URI [/ webapp / trainees]的HTTP请求未找到映射 对于所有应该由
我正在尝试执行简单的hibernate java程序- 使用的文件:hibernate。cfg。xml,员工。cfg。Java类:HibernateUtil。爪哇,员工们。爪哇,性别。java(Enum),InsertIntoEmployee。JAVA InsertIntoEmployee的代码。JAVA 获取以下错误: Classpath条目如下: 在过去的三天里,我一直在讨论这个问题。 谷歌搜
当我打开我的android应用程序并测试解析推送通知时,它工作了。但是当我把我的应用程序从多任务中杀死并再次测试时,应用程序崩溃了。 错误日志 08-18 21:16:21.694 244 06-24406/?E/AndroidRuntime:致命异常:main process:com.myatminsoe.mKeyboard,PID:24406 java.lang.runtimeExceptio