2011-05-03 166 views
0

我可以在我的HBM映射中執行以下操作嗎?遞歸HBM映射

<class name="Employee" table="employees"> 
    <!-- assume that each person only has exactly one supervisor --> 
    <many-to-one name="supervisor" class="Employee" column="supervisorId" /> 
</class> 

當我使用上述HBM映射,我的服務器拒絕啓動並出現以下錯誤:

org.hibernate.InstantiationException: could not instantiate test object Employee 
Caused by: java.lang.StackOverflowError 
at Employee.<init>(Employee.java:11) 
at Employee.<init>(Employee.java:11) 
at Employee.<init>(Employee.java:11) 
...... (about a hundred duplicates) 

線Employee.java的11只說:

public class Employee implements Serializable { 

應該如何我建模我的主管 - 員工關係?主管沒有專門的POJO,管理對象沒有專門的領域。

回答

1

Hibernate應該沒有映射這種關係的問題。

它看起來像無限遞歸是錯誤代碼造成的,是這樣的:

public class Employee { 
    private Employee supervisor = new Employee(); 
} 
+0

是的,我的代碼,準確的線。我將其更改爲私人Employee supervisor = null,現在它可以工作。 – David 2011-05-03 16:27:33