我正在尝试使用此测试项目首次使用hibernate实现设置JPA。遇到以下错误:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ExamModulePu] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at service.tests.ExamServiceTest.main(ExamServiceTest.java:18)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Question.examId references an unknown entity: long
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1536)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1457)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1365)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
这些是我的实体:
@Entity
public class Exam implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private int numQuestions;
private int minutesAllotted;
private Date startDate;
private Date endDate;
@OneToMany(mappedBy = "examId", orphanRemoval = true)
private List<Question> questionList;
// Getters and setters here..
}
@Entity
public class Question implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@ManyToOne
private long examId;
private int points;
private int timeLimit;
private String text;
private String category;
@Embedded
private List<Answer> answerList;
}
@Embeddable
public class Answer implements Serializable {
private int optionNumber;
private String text;
private boolean correct;
}
这就是我的persistence.xml的样子:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="ExamModulePu"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.Exam</class>
<class>entities.Question</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/exammoduledb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
我没有创建任何表格,而是取决于hibernate.hb2mddl.auto
这样做。但我相信我的错误甚至在发生之前就会出现,因为它无法生成持久性单元。我有什么想法我做错了吗?我确保只导入javax.persistence.*;
答案 0 :(得分:65)
如果仔细观察堆栈跟踪,您会看到
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Question.examId references an unknown entity: long
所以这个领域
@ManyToOne
private long examId;
造成了这个问题。 @ManyToOne
有一个参数targetEntity
,其中说:
(可选)作为关联目标的实体类。 默认为存储关联的字段或属性的类型。
由于您尚未提供该参数,因此默认为long
,而不是托管Entity
。
您可能想要使用
@ManyToOne(targetEntity = Exam.class)
private long examId;
否则它将不知道要映射到什么。或者甚至更好
@ManyToOne
private Exam exam;
答案 1 :(得分:16)
只需将 Team 类添加到“hibernate-cfg.xml”文件中,因为Hibernate在没有添加的情况下无法识别。
答案 2 :(得分:9)
仅供参考,如果您有一个休眠注释,有时会发生这种情况:
@org.hibernate.annotations.Entity
和JPA注释:
@javax.persistence.Entity
混淆
编辑:
显式导入javax注释。
答案 3 :(得分:-1)
我花了几个小时才找到实际问题
我忘记了 Parent 类上的 @Entity 注释以及用于显示在子类上的错误。
答案 4 :(得分:-1)
就我而言,Hibernate 无法识别实体,因为我没有使用 .addAnnotatedClass(XYZ.class) 在 sessionfactory 中添加该实体,因此无法找到该特定实体。