什么表结构使用(休眠) [英] What table structure to use (hibernate)
问题描述
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屋!