什么表结构使用(休眠) [英] What table structure to use (hibernate)

查看:178
本文介绍了什么表结构使用(休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体:PhisicalPerson(PP),JuredicalPerson(JP)。我想创建电话对象。 JP有很多电话,PP有很多电话(一对多关系)。所以在Phone对象中,我必须为这个关系创建2列:

  class Phone {

@ JoinColumn(name =ppId,nullable = true)
@ManyToOne
public PhisicalPerson getPhisicalPerson(){...}

@JoinColumn(name =jpId,nullable = true)
@ManyToOne
public JuredicalPerson getJuredicalPerson(){...}

//数字,城市代码,附加数字和其他字段

}

是否正确实施?或者可以创建不同的实体:PhisicalPersonPhone和JuredicalPersonPhone?

这确实是你可以做的,但它看起来像你在这里有一个继承关系。



PP和JP都应该扩展一个通用的基本实体Person,它是Person实体,它应该有一个电话列表。电话实体将只与Person有一个ManyToOne关联。



假设电话实体需要知道其拥有的人。也许单向关联在这里会更有意义。在这种情况下,使用连接表(或两个,如果你不想要这种继承关系),会更有意义。


I hava two entities: PhisicalPerson (PP), JuredicalPerson (JP). And I want to create Phone object. JP has many phones and PP has many phones (one to many relation). So in Phone object I have to create 2 columns for this relations:

class Phone {

 @JoinColumn(name="ppId",nullable=true)
 @ManyToOne
 public PhisicalPerson getPhisicalPerson() {...}

 @JoinColumn(name="jpId",nullable=true)
 @ManyToOne
 public JuredicalPerson getJuredicalPerson() {...}

 // number, city code, additional number and other fields

}

Is it right implementation? Or may be it's better to create different entities: PhisicalPersonPhone and JuredicalPersonPhone?

解决方案

That's indeed what you could do, but it looks like you have an inheritance relationship here.

Both PP and JP should probably extend a common base entity Person, and it's the Person entity which should have a list of phones. The Phone entity would then just have one ManyToOne association with Person.

That's assuming that the Phone entity needs to know about its owning person. Maybe a unidirectional association would make more sense here. In this case, using a join table (or two, if you don't want this inheritance relationship), would make more sense.

这篇关于什么表结构使用(休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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