如何使用 JPA 对此进行建模?
我想对几个对象关系进行建模,但目前我不确定是否有明智的方法来做到这一点。我们假设一个 Record
与不同的 RecordSource 具有 OneToMany 关系。 RecordSources
的公共属性是long id
和boolean Preferred
。其他属性是个性化的,记录源的数量将来可能会增加。
一种可能性是 Record
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="record")
private List<GenericSimpleRecordSource> recordSources;
,其中 GenericSimpleRecordSource
如下所示:
@Entity
public class GenericSimpleRecordSource implements Serializable
{
public static enum Type {a,b}
@ManyToOne private Record record;
@NotNull
private Type sourceType;
@OneToOne(cascade = CascadeType.ALL,mappedBy="source")
private SimpleRecordSourceA sourceA;
@OneToOne(cascade = CascadeType.ALL,mappedBy="source")
private SimpleRecordSourceB sourceB;
}
SimpleRecordSourceA
和 SimpleRecordSourceA
是单独的 @Entity
类。我对这种方法感到不舒服,使用继承可能会更好:
@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="type")
@DiscriminatorValue("generic")
public class GenericRecordSource
{
@Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
@ManyToOne private Record record;
private boolean preferred;
}
@Entity
@DiscriminatorValue("A")
public class RecordSourceA extends GenericRecordSource
{
...
}
@Entity
@DiscriminatorValue("B")
public class RecordSourceB extends GenericRecordSource
{
...
}
这似乎更聪明,但是使用继承有什么缺点吗?我感谢对这两种方法甚至另一种替代方案的任何评论。
是否可以像这样从 Record
建模 @OneToMany
关系?
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="record")
private List<GenericRecordSource> recordSources;
I want to model a couple of object relations and I'm currently not sure about a smart way to do this. Let's assume a Record
has a OneToMany relationship to different RecordSources. The common attributes of the RecordSources
are long id
and boolean preferred
. Other attributes are individual and the number of record sources may increase in the future.
One possibility is for Record
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="record")
private List<GenericSimpleRecordSource> recordSources;
where GenericSimpleRecordSource
would look like this:
@Entity
public class GenericSimpleRecordSource implements Serializable
{
public static enum Type {a,b}
@ManyToOne private Record record;
@NotNull
private Type sourceType;
@OneToOne(cascade = CascadeType.ALL,mappedBy="source")
private SimpleRecordSourceA sourceA;
@OneToOne(cascade = CascadeType.ALL,mappedBy="source")
private SimpleRecordSourceB sourceB;
}
SimpleRecordSourceA
and SimpleRecordSourceA
are individual @Entity
classes. I don't feel comfortable with this approach, using inheritance might be better:
@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="type")
@DiscriminatorValue("generic")
public class GenericRecordSource
{
@Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
@ManyToOne private Record record;
private boolean preferred;
}
@Entity
@DiscriminatorValue("A")
public class RecordSourceA extends GenericRecordSource
{
...
}
@Entity
@DiscriminatorValue("B")
public class RecordSourceB extends GenericRecordSource
{
...
}
This seems to be smarter, but are there any shortcomings using Inheritance? I appreciate any comments on both approaches or even another alternative.
Is it possible to model the @OneToMany
relationship from Record
like this?
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="record")
private List<GenericRecordSource> recordSources;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论