2011-05-03 91 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