2013-06-12 10 views
23

Próbuję ustawić JPA z implementacją hibernacji po raz pierwszy w tym projekcie testowym. Wpadł na następujący błąd:org.hibernate.AnnotationException: @OneToOne lub @ManyToOne na entity.Ques # tion.examId odwołuje się do nieznanego obiektu: 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) 

To są moje podmioty:

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

To jest to, co mój persistence.xml wygląda następująco:

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

nie zostały utworzone żadne z tabele, ale zamiast tego jestem zależny od tego, aby to zrobić. Wierzę jednak, że mój błąd pojawia się jeszcze przed tym, ponieważ nie może wygenerować jednostki wytrwałości. Jakieś pomysły, co robię źle? Ja upewniając importować tylko javax.persistence.*;

Odpowiedz

52

Jeśli przyjrzeć się bliżej swojej stacktrace, widać

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

Więc to pole

@ManyToOne 
private long examId; 

jest przyczyną problemu. @ManyToOne ma paramater targetEntity który mówi:

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

Ponieważ nie dostarczyły tego parametru, domyślnie long, który nie jest zarządzany Entity.

Będziesz prawdopodobnie chcesz użyć

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

inaczej nie będzie wiedział, co do mapowania. nawet lepiej

@ManyToOne 
private Exam exam; 
12

lub Wystarczy dodać klasa Zespół do pliku „hibernate-cfg.xml”, ponieważ Hibernacja nie identyfikuje bez dodawania do niego.

8

FYI, to czasami zdarza się, jeśli masz adnotacji Hibernacja:

@org.hibernate.annotations.Entity 

i adnotacji WZP:

@javax.persistence.Entity 

zmieszany się

Edit:

Jawnie importować Adnotacja javax.

+1

jaka jest sugestia @CoffeJunky? – sisimh

+0

Jego sugestia dotyczy wyłącznie importu 'javax.persistence.Entity' zamiast' org.hibernate.annotations.Entity'. –

+0

To nie jest w przypadku mojego programu, ale nadal daje błąd – Avdhut

Powiązane problemy