如何使用 JPA 对此进行建模?

发布于 2024-11-15 13:47:59 字数 1830 浏览 2 评论 0原文

我想对几个对象关系进行建模,但目前我不确定是否有明智的方法来做到这一点。我们假设一个 Record 与不同的 RecordSource 具有 OneToMany 关系。 RecordSources 的公共属性是long idboolean 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;
}

SimpleRecordSourceASimpleRecordSourceA 是单独的 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文