Hibernate映射:OneToMany和OneToOne在子对象属性上的使用 [英] Hibernate mapping: OneToMany and OneToOne on child object property

查看:219
本文介绍了Hibernate映射:OneToMany和OneToOne在子对象属性上的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是父类Enterprise。它有雇主,其中一个是企业总裁。

  @Entity 
类企业
{
//字段

@OneToMany
公共列表< Employee> getEmployers()
//执行

@OneToOne
public Employee getPresident()
//执行


这是child Employee类。它只有关于他工作的企业的信息。但问题是我应该使用哪种关联?

  @Entity 
类员工
{
//字段

//我应该使用哪种关联?
public Enterprise getEnterprise()
//执行
}


解决方案

鉴于您定义了企业 - > 雇主 @OneToMany ,这意味着雇主只属于一个企业 >,你应该使用 @ManyToOne ,这意味着每一个雇主都属于最大值。 1 企业,但是企业可以引用许多雇主

您可以使用 mapped-by

  @Entity 
class企业
{
@OneToMany (映射=企业)
公共列表<员工> getEmployers()
//执行

@OneToOne
public Employee getPresident()
//执行
}

@ Entity
class Employee
{
@ManyToOne
@JoinTable(name =Enterprise,joinColumns = {@JoinColumn(name =ENT_ID,referencedColumnName =ENT_ID)}
public Enterprise getEnterprise()
//执行
}

如果雇主可以担任其职位的其他 Enterprise> / >的总裁(似乎不太可能,除非可以如果您需要访问其中雇主 Enterprise 是来自雇主实体的总裁,您需要添加另一个关联,理想情况下使用 @OneToOne (您会遇到问题,因为@OneToOne关系需要两个实体具有相同的@Id类)。在这种情况下,我会anno使用 @ManyToOne 雇主上的 getPresidedEnterprise()出于实际的原因。

Here is parent class Enterprise. It has employers and one of them is president of enterprise.

@Entity
class Enterprise
{
   // fields

   @OneToMany
   public List<Employee> getEmployers()
   // implementation    

   @OneToOne
   public Employee getPresident()
   // implementation

}

Here is child Employee class. It has only info about Enterprise where he works. But question is what association should I use?

@Entity
class Employee 
{
   // fields

   // what association should I use?
   public Enterprise getEnterprise()
   // implementation
}

解决方案

Given that you've defined the Enterprise->Employers association with @OneToMany, which means that an Employer belongs to only one Enterprise, you should be using @ManyToOne, meaning that every Employer belongs to max. 1 Enterprise, but an Enterprise can reference many Employers.

You can define the association specifics (join columns, etc) in one of the sides only, using the mapped-by attribute in the annotation:

@Entity
class Enterprise
{
   @OneToMany(mapped-by="enterprise")
   public List<Employee> getEmployers()
   // implementation    

   @OneToOne
   public Employee getPresident()
   // implementation
}

@Entity
class Employee 
{
   @ManyToOne
   @JoinTable ( name="Enterprise", joinColumns={ @JoinColumn(name="ENT_ID", referencedColumnName="ENT_ID") }
   public Enterprise getEnterprise()
   // implementation
}

In case an Employer could be president of a different Enterprise in which he is employed (seems unlikely, unless one can be president of an enterprise without being employed by it), and in case you needed to access the Enterprise of which the Employer is president from the Employer entity, you would need to add another association, ideally with @OneToOne (you would encounter problems, because @OneToOne relations require both entities to have the same @Id class). In this case I would annotate the getPresidedEnterprise() method on Employer with @ManyToOne for practical reasons.

这篇关于Hibernate映射:OneToMany和OneToOne在子对象属性上的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆