递归 HBM 映射

发布于 2024-11-04 11:00:31 字数 791 浏览 3 评论 0原文

我可以在 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,并且主管对象没有特殊字段。

Can I do the following in my HBM mapping?

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

When I use the above HBM mapping, my server refuses to start with the following error:

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)

line 11 of Employee.java merely says:

public class Employee implements Serializable {

How should I model my supervisor-employee relationship? There is no special POJO for supervisor and supervisor objects have no special fields.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2024-11-11 11:00:32

Hibernate 在映射这种关系时不应该有问题。

看起来无限递归是由代码中的错误引起的,如下所示:

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

Hibernate shouldn't have a problem with mapping such a relationship.

It looks like infinite recursion is caused by error in your code, something like this:

public class Employee {
    private Employee supervisor = new Employee();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文