41

我正在尝试使用这个测试项目首次使用休眠实现设置 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.*;

4

5 回答 5

77

如果您仔细查看您的堆栈跟踪,您会看到

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;
于 2013-06-12T22:42:44.463 回答
21

只需将类Team添加到“hibernate-cfg.xml”文件中,因为 Hibernate 不添加就无法识别。

于 2013-09-03T06:43:34.290 回答
10

仅供参考,如果您有休眠注释,有时会发生这种情况:

@org.hibernate.annotations.Entity

和一个 JPA 注释:

@javax.persistence.Entity 

混合

编辑:

显式导入 javax 注释。

于 2013-09-30T17:13:42.033 回答
3

我花了几个小时才找到实际问题

我忘记了父类上的@Entity 注释以及用于在子类上显示的错误。

于 2021-02-16T06:58:51.790 回答
-1

在我的情况下,Hibernate 无法识别实体,因为我没有使用 .addAnnotatedClass(XYZ.class) 在 sessionfactory 中添加该实体,因此它无法找到该特定实体。

于 2021-02-27T10:53:33.397 回答