2013-06-12 64 views
23

我試圖用這個測試項目第一次設置JPA與hibernate實現。陶然成以下錯誤:org.hibernate.AnnotationException:@OneToOne或@ManyToOne對entities.Ques#tion.examId引用一個未知的實體: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) 

這是我的實體:

@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.*;

回答

52

如果您在您的堆棧跟蹤仔細觀察,你會看到

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

所以本場

@ManyToOne 
private long examId; 

引起的問題。 @ManyToOne有paramater 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.

因爲你沒有提供的參數,它默認爲long,這不是一個管理Entity

您可能需要使用

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

否則將不知道如何映射到。甚至更好

@ManyToOne 
private Exam exam; 
12

只需添加到「休眠-cfg.xml中」文件,因爲Hibernate不無添加進去辨認。

@org.hibernate.annotations.Entity 

和JPA註釋:

@javax.persistence.Entity 

混合起來

編輯:

明確導入

8

僅供參考,如果你有一個休眠註釋這有時會發生javax註釋。

+1

你的建議@CoffeJunky是什麼? – sisimh

+0

他的建議是專門導入'javax.persistence.Entity'而不是'org.hibernate.annotations.Entity'。 –

+0

這不是我的程序的情況下,但仍然給錯誤 – Avdhut

相關問題