web-dev-qa-db-fra.com

org.hibernate.AnnotationException: @OneToOne ou @ManyToOne sur entity.Ques # tion.examId fait référence à une entité inconnue: long

J'essaie de configurer JPA avec une implémentation hibernate pour la première fois avec ce projet de test. Ran dans l'erreur suivante:

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)

Ce sont mes entités:

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

Voici à quoi ressemble mon 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>

Je n'ai créé aucune des tables, mais à la place, je dépend du hibernate.hb2mddl.auto faire cela. Mais je crois que mon erreur se produit avant même que cela ne se produise car elle ne peut pas générer l'unité de persistance. Des idées sur ce que je fais mal? Je m'assure d'importer uniquement javax.persistence.*;

33

Si vous regardez attentivement votre stacktrace, vous verrez

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

Donc ce champ

@ManyToOne
private long examId;

est à l'origine du problème. @ManyToOne a un paramètre targetEntity qui dit:

(Facultatif) La classe d'entité qui est la cible de l'association. Par défaut, le type du champ ou de la propriété qui stocke l'association.

Étant donné que vous n'avez pas fourni ce paramètre, il est par défaut long, qui n'est pas un Entity géré.

Vous voudrez probablement utiliser

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

sinon, il ne saura pas sur quoi mapper. Ou encore mieux

@ManyToOne
private Exam exam;
65

Ajoutez simplement la classe Team au fichier "hibernate-cfg.xml", car Hibernate ne s'identifie pas sans y ajouter.

17
Chintan Panchal

Pour info, cela se produit parfois si vous avez une annotation d'hibernation:

@org.hibernate.annotations.Entity

et une annotation JPA:

@javax.persistence.Entity 

mélangé

Modifier:

Importez explicitement l'annotation javax.

10
CoffeJunky