2013-06-12 49 views
23

Tôi đang cố thiết lập JPA với triển khai hibernate lần đầu tiên với dự án thử nghiệm này. Chạy vào các lỗi sau:org.hibernate.AnnotationException: @OneToOne hoặc @ManyToOne trên thực thể.Ques # tion.examId tham chiếu đến một thực thể không xác định: dài

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) 

Đây là những thực thể của tôi:

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

Đây là những gì persistence.xml của tôi trông giống như:

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

tôi đã không tạo ra bất kỳ bảng, nhưng thay vào đó phụ thuộc vào hibernate.hb2mddl.auto để làm như vậy. Nhưng tôi tin rằng lỗi của tôi sẽ xảy ra ngay cả trước khi điều đó xảy ra vì nó không thể tạo ra đơn vị bền vững. Bất kỳ ý tưởng những gì tôi đang làm sai? Tôi đảm bảo tôi nhập khẩu chỉ javax.persistence.*;

Trả lời

52

Nếu bạn nhìn kỹ vào stacktrace của bạn, bạn sẽ thấy

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

Vì vậy, lĩnh vực này

@ManyToOne 
private long examId; 

gây ra vấn đề. @ManyToOne có một paramater targetEntity mà nói:

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

Vì bạn chưa cung cấp rằng tham số, nó mặc định là long, mà không phải là một quản lý Entity.

Có thể bạn sẽ muốn sử dụng

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

nếu không nó sẽ không biết để lập bản đồ những gì để. Hoặc thậm chí tốt hơn

@ManyToOne 
private Exam exam; 
12

Chỉ cần thêm lớp Đội đến "ngủ đông-cfg.xml" tập tin, vì Hibernate không xác định mà không cần thêm vào nó.

8

FYI, điều này đôi khi sẽ xảy ra nếu bạn có một chú thích ngủ đông:

@org.hibernate.annotations.Entity 

và một chú thích JPA:

@javax.persistence.Entity 

lẫn lộn

Edit:

Rõ ràng nhập chú thích javax.

+1

đề xuất ur là gì @CoffeJunky? – sisimh

+0

Đề xuất của ông là nhập khẩu độc quyền 'javax.persistence.Entity' thay vì' org.hibernate.annotations.Entity'. –

+0

Đây không phải là trường hợp của chương trình của tôi nhưng vẫn đưa ra lỗi – Avdhut