2012-10-21 66 views
-1

我之前問過一個問題,但認爲它不夠清楚,我將詳細闡述代碼。
我有一個教師實體和部門實體
許多教師屬於一個部門並
一個部門有一個負責部門的教師。
下面是我如何實現它。JPA OneToOne和ManyToMany兩個實體之間

@Entity 
public class Teacher extends Model { 
@Required 
public String surname; 

@Required 
public String othernames; 

    ... 

@OneToOne(mappedBy = "deptHead") 
public Department headedBy = new Department(); 

@ManyToOne(cascade=CascadeType.ALL) 
public Department dept = new Department(); 

... 
} 

和部門實體

@Entity 
public class Department extends Model { 
@Required 
public String deptName; 

@OneToMany(mappedBy="dept") 
public List<Teacher> teachers = new ArrayList<Teacher>(); 

@OneToOne 
public Teacher deptHead = new Teacher(); 

    ... 

} 

我得到以下

@6c4nj8nmg 
    Internal Server Error (500) for request GET/

    JPA error 
    A JPA error occurred (Unable to build EntityManagerFactory): could not instantiate test objectmodels.Department 

    play.exceptions.JPAException: Unable to build EntityManagerFactory 
    at play.db.jpa.JPAPlugin.onApplicationStart(JPAPlugin.java:269) 
    at play.plugins.PluginCollection.onApplicationStart(PluginCollection.java:525) 
    at play.Play.start(Play.java:526) 
    at play.Play.detectChanges(Play.java:630) 
    at play.Invoker$Invocation.init(Invoker.java:198) 
    at Invocation.HTTP Request(Play!) 
    Caused by: org.hibernate.InstantiationException: could not instantiate test objectmodels.Department 
    at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:48) 
    at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:67) 
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67) 
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135) 
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485) 
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133) 
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84) 
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906) 
    at play.db.jpa.JPAPlugin.onApplicationStart(JPAPlugin.java:267) 
    ... 5 more 
    Caused by: java.lang.reflect.InvocationTargetException 
    at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:45) 
    ... 15 more 
    Caused by: java.lang.StackOverflowError 
    at java.lang.Class.searchMethods(Unknown Source) 
    at java.lang.Class.getMethod0(Unknown Source) 
    at java.lang.Class.getMethod(Unknown Source) 
    at  play.classloading.enhancers.PropertiesEnhancer$FieldAccessor.invokeWriteProperty(PropertiesEnhancer.java:268) 
    at models.Department.<init>(Department.java:23) 
    at models.Teacher.<init>(Teacher.java:47) 
    at models.Department.<init>(Department.java:26) 
    ... 

需求的錯誤幫助這些

回答

3

這不是一個JPA的問題,而是由引起遞歸實例化Teacher/Department

當您創建,或詢問JPA創建的Teacher的情況下,Teacher嘗試實例化一個Department,它實例化一個Teacher ...,到無限遠。

因此,您看到該堆棧跟蹤結束附近出現StackOverflowError錯誤。

從類定義中刪除= new Teacher()= new Department()表達式;在創建它們時依賴於並使用適當的setter方法。

+0

謝謝Richard Sitze,我已經糾正了遞歸實例化,很快就會顯示糾正和工作代碼。 –

0

我刪除了Teacher和Department之間的@oneToOne關係,並創建了一個名爲DepartmentHeads的新實體,現在一切正常。
以下是結果。

@Entity 
public class Teacher extends Model { 
    @Required 
    public String surname; 

    @Required 
    public String othernames; 

    ... 

    @ManyToOne(cascade=CascadeType.ALL) 
    public Department dept = new Department(); 

    ... 
} 

@Entity 
public class Department extends Model { 
    @Required 
    public String deptName; 

    @OneToMany(mappedBy="dept") 
    public List<Teacher> teachers = new ArrayList<Teacher>(); 

    ... 

} 

@Entity 
public class DepartmentHead extends Model{ 
    @OneToOne 
    public Teacher teacher = new Teacher(); 

    @OneToOne 
    public Department dept = new Department(); 
} 

現在一切正常。