2013-06-12 12 views
23

Sto provando a configurare JPA con un'implementazione di ibernazione per la prima volta con questo progetto di test. Corse nel seguente errore:org.hibernate.AnnotationException: @OneToOne o @ManyToOne su entity.Ques # tion.examId fa riferimento a un'entità sconosciuta: long

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) 

Questi sono i miei soggetti:

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

Questo è ciò che il mio persistence.xml assomiglia:

<?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> 

non ho creato una delle tabelle, ma invece sono in funzione dello hibernate.hb2mddl.auto per farlo. Ma credo che il mio errore si verifichi ancora prima che ciò accada perché non può generare l'unità di persistenza. Qualche idea su cosa sto facendo male? Sto facendo sicuro di importare solo javax.persistence.*;

risposta

52

Se si guarda da vicino al vostro stacktrace, vedrete

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

Quindi questo campo

@ManyToOne 
private long examId; 

è la causa del problema. @ManyToOne ha un paramater targetEntity che dice:

(Optional) The entity class that is the target of the association. Defaults to the type of the field or property that stores the association.

Poiché non hai fornito tale parametro, il valore predefinito è long, che non è una gestito Entity.

probabilmente si vorrà utilizzare

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

altrimenti non saprà cosa da mappare. O ancora meglio

@ManyToOne 
private Exam exam; 
12

Basta aggiungere la classe squadra al file "hibernate-cfg.xml", perché Hibernate non identifica senza aggiungere in esso.

8

FYI, questo a volte succede se si dispone di un'annotazione hibernate:

@org.hibernate.annotations.Entity 

e un'annotazione di JPA:

@javax.persistence.Entity 

mescolato

Edit:

esplicitamente importare il annotazione javax.

+1

qual è il tuo suggerimento @CoffeJunky? – sisimh

+0

Il suo suggerimento è di importare esclusivamente 'javax.persistence.Entity' invece di' org.hibernate.annotations.Entity'. –

+0

Questo non è il caso nel mio programma ma continua a dare l'errore – Avdhut

Problemi correlati