gpt4 book ai didi

java - 一个实体类映射几个表

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:01 24 4
gpt4 key购买 nike

我的项目中有一个非常重要的表 - 强制表。它有几列,但在本例中最重要的是主键。

Mandatory
1. id (Long Primary Key)
...

现在我必须添加两个新表 - 附加表和额外表。它们都只有两列。在附加中,两者都是主键。如您所见,其中之一也是外键。

Additional
1. mandatory_id (Long Primary Key FK -> Mandatory.id)
2. description (Varchar(512) Primary Key)

在 Extra 表中主键也是外键。

Extra
1. mandatory_id (Long Primary Key FK -> Mandatory.id)
2. text (Varchar(512)

我正在考虑如何使用 Hibernate 在我的项目中实现它。

我以这种方式声明了它们(这里没有 getter、setter 等),MandatoryEntity 类:

@Entity
@Table(name = "mandatory")
public class MandatoryEntity {

@OneToMany
@JoinColumn(name = "mandatory_id")
protected List<AdditionalEntity> additonals;

@OneToOne
@JoinColumn(name = "mandatory_id")
protected ExtraEntity extra;
}

AdditionalEntity 类:

@Entity
@Table(name = "additional")
public class AdditionalEntity {

@EmbeddedId
private AdditionalPK id;

@ManyToOne(fetch = FetchType.LAZY)
@Fetch(value = FetchMode.JOIN)
@JoinColumn(name = "mandatory_id", nullable = false, insertable = false, updatable = false)
protected MandatoryEntity mandatory;
}

AdditionalPK 类:

@Embeddable
public class AdditionalPK {

@Column(name = "mandatory_id")
private Long mandatoryId;

@Column(name = "description")
private String description;
}

ExtraEntity 类:

@Entity
@Table(name = "extra")
public class ExtraEntity {

@Id
@Column(name = "mandatory_id")
private Long id;

@OneToOne
@PrimaryKeyJoinColumn
protected MandatoryEntity mandatory

@Column(name = "text")
protected String text;
}

此时我正在使用的笔记本电脑坏了,所以我有时间考虑。我在 JPA 规范 @SecondaryTables 注释中找到了。我可以在我的案例中使用这个注释吗?这些只是两个表,每个表只有两列。第一个有复合主键,第二个没有。

也许还有另一种方法可以在一个 MandatoryEntity 中实现这些表?没有两个新实体:AdditionalEntityExtraEntity

最佳答案

首先,让我重新表述一下我从您的要求中了解到的内容。

  1. 您有一个主要实体(强制)
  2. 您还有另外两个实体向强制实体指定附加信息。其中

    a.可以指定多个附加信息(ManyToOne)

    b.可以指定一个额外信息(OneToOne)

通常这是一个非常基本的用例。您必须在这里决定的一件事是您是否需要双向映射或单向映射(仅通过强制实体访问)就足够了。我希望单向映射对于您的用例来说是一个不错的选择,因为看起来 Mandatory 是您的根实体。在您的代码中,您尝试对双向关系进行建模,但忘记指定逆关系。这实际上会产生两个独立的关系。您应该像这样指定双向关系的目标:


@Entity
@Table(name = "mandatory")
public class MandatoryEntity {
// you need a mapped by to mark the relation as bidirectional
@OneToMany(mappedBy = "mandatory")
protected List additonals;
@OneToOne
protected ExtraEntity extra;
}<p></p>

<p>@Entity
@Table(name = "additional")
public class AdditionalEntity {
@EmbeddedId
private AdditionalPK id;
@ManyToOne(fetch = FetchType.LAZY)
protected MandatoryEntity mandatory;
}</p>

<p></p>

您还指定了示例中不需要的许多附加映射信息,这使得一切看起来都过于复杂。还为什么要使用嵌入式 key ?我认为自动生成的 key 也适合您。我对您的模型的建议如下:


@Entity
@Table(name = "mandatory")
public class MandatoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// you need a mapped by to mark the relation as bidirectional
@OneToMany(mappedBy = "mandatory")
protected List additonals;
@OneToOne
protected ExtraEntity extra;
}
@Entity
@Table(name = "additional")
public class AdditionalEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
protected MandatoryEntity mandatory;
}
@Entity
@Table(name = "extra")
public class ExtraEntity {
// you might also not use auto-value here and set the id to the id of the owning
// mandatory entity
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "text")
protected String text;
}

如果我遗漏了一些要求,并且我建议的映射不适用于您的情况,那么请添加有关您的要求的更多信息,我们可以进一步讨论这个问题。

关于java - 一个实体类映射几个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com