本文介绍了如何使用JPA(Hibernate)创建与同一个实体的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体 User ,它应该有属性 manager ,其中manager是另一个用户(一位经理可以管理许多用户,任何用户可能只有1个经理或没有任何)。

I have an entity User and it should have property manager where manager is another user (one manager can manage many users, any user may have only 1 manager or have not any).

我怎么能实现这个?

我尝试了一些标准

@ManyToOne
@JoinColumn (name = ??? /* what should be here? */, nullable = true)
private User manager;

但它并不像看起来那么简单..

but it's not as simple as it seems..

推荐答案

有什么问题?如果您不知道如何命名连接列(应该默认为MANAGER_ID),请使用默认值,即不要设置名称。从 <$ c的javadoc

What's the problem? Use the default value i.e. don't set the name if you don't know how to name the join column (should default to something like MANAGER_ID). From the javadoc of the name attribute:

这篇关于如何使用JPA(Hibernate)创建与同一个实体的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多