简体   繁体   English

org.hibernate.AnnotationException:entities.Ques#tion.examId 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:long

[英]org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Ques#tion.examId references an unknown entity: long

I'm trying to setup JPA with a hibernate implementation for the first time with this test project.我正在尝试使用此测试项目第一次使用休眠实现设置 JPA。 Ran into the following error:遇到以下错误:

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)

These are my entities:这些是我的实体:

@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;
}

This is what my persistence.xml looks like:这是我的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>

I haven't created any of the tables, but instead am depending on the hibernate.hb2mddl.auto to do so.我还没有创建任何表,而是依赖于hibernate.hb2mddl.auto来这样做。 But I believe my error springs up even before that happens as it can't generate the persistence unit.但我相信我的错误甚至在发生之前就出现了,因为它无法生成持久性单元。 Any ideas what I'm doing wrong?任何想法我做错了什么? I'm making sure I import only javax.persistence.*;我确保我只导入javax.persistence.*;

If you look closely at your stacktrace, you will see如果您仔细查看堆栈跟踪,您会看到

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Question.examId references an unknown entity: long

So this field所以这个领域

@ManyToOne
private long examId;

is causing the problem.导致问题。 @ManyToOne has a paramater targetEntity which says: @ManyToOne有一个参数targetEntity ,它说:

(Optional) The entity class that is the target of the association. (可选)作为关联目标的实体类。 Defaults to the type of the field or property that stores the association.默认为存储关联的字段或属性的类型。

Since you haven't provided that parameter, it defaults to long , which is not a managed Entity .由于您尚未提供该参数,因此它默认为long ,它不是托管Entity

You'll probably want to use你可能想要使用

@ManyToOne(targetEntity = Exam.class)
private long examId;

otherwise it won't know what to map to.否则它不知道要映射到什么。 Or even better或者更好

@ManyToOne
private Exam exam;

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

FYI, this sometimes happens if you have a hibernate annotation:仅供参考,如果您有休眠注释,有时会发生这种情况:

@org.hibernate.annotations.Entity

and a JPA annotation:和 JPA 注释:

@javax.persistence.Entity 

mixed up混合

Edit:编辑:

Explicitly import the javax annotation.显式导入 javax 注释。

It took me hours to find the actual issue我花了几个小时才找到实际问题

I had forgotten the @Entity annotation on the Parent class and the error use to show on the child class.我忘记了 Parent 类上的 @Entity 注释和用于显示在子类上的错误。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体 - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity Java Hibernate org.hibernate.AnnotationException:@OneToOne或@ManyToOne <class> 引用未知实体: <class> - Java Hibernate org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <class> references an unknown entity: <class> 由以下原因引起:org.hibernate.AnnotationException:XX上的@OneToOne或@ManyToOne引用了未知实体:YY - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on XX references an unknown entity: YY 不断获取 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne 引用未知实体:错误 - Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error 引起:org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne - org.hibernate.AnnotationException: @OneToOne or @ManyToOne org.hibernate.AnnotationException:@OneToOne on com.abc.dto.Test.name 引用了一个未知实体:com.abc.type.TestName - org.hibernate.AnnotationException:@OneToOne on com.abc.dto.Test.name references an unknown entity: com.abc.type.TestName 线程“ main” org.hibernate.AnnotationException中的异常:@OneToOne或@ManyToOne - Exception in thread “main” org.hibernate.AnnotationException: @OneToOne or @ManyToOne 线程“ main” org.hibernate.AnnotationException中的异常:@OneToOne或@ManyToOne - Exception in thread “main” org.hibernate.AnnotationException: @OneToOne or @ManyToOne on AnnotationException:org.am.Car.engine上的@OneToOne或@ManyToOne引用了未知实体:org.am.Engine - AnnotationException: @OneToOne or @ManyToOne on org.am.Car.engine references an unknown entity: org.am.Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM