gpt4 book ai didi

mysql - 使用 JPA 将连接表映射到 Java 中的 Map 对象

转载 作者:行者123 更新时间:2023-11-29 00:48:09 25 4
gpt4 key购买 nike

我有五个 MySQL 表

  • employee
  • qualification
  • institute qualification_institute (id, qualification_id,institute_id)
  • employee_qualification_institute (employee_id, qualification_institute_id)

qualification_institute employee_qualification_institute 定义了可获得的资格和机构之间的多对多连接告诉我们特定员工在哪个机构获得了他/她的资格。 EclipseLink 生成一个 Set<QualificationInstitute> Employee 中的字段类,但是我想要Map<Qualification, Institute>而不是它。我应该如何注释该 map ?由于我具有基于属性的访问权限,我是否需要在其 getter 方法中实现查询?

Qualification类已经包含一个 Set<Institute>场而类Institute持有Set<Qualification> .如果我可以在 Employee 中注释 map ,也许我可以避免必须有一个明确的 EmployeeQualificationInstitute类。

最佳答案

在这种情况下你不能。由于按照设计,每个学院的资格证书并不是唯一的。我引用了 Map 的 JavaDoc:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

然而,EmployeeQualificationInstitute 类不是必需的。

例子:

@Entity
public class Employee
{
@Id
@GeneratedValue
private Long id;
@JoinTable(name="EmployeeQualificationInstitute",
joinColumns={
@JoinColumn(name = "id", referencedColumnName = "employee_id")
}
)
private Set<QualificationInstitute>
}

@Entity
public class Qualification
{
@Id
@GeneratedValue
private Long id;
}

@Entity
public class Institute
{
@Id
@GeneratedValue
private Long id;
}

@Entity
public class QualificationInstitute
{
@EmbeddedId
private QualificationInstitutePK key;

@MapsId(value = "qualification_id")
@ManyToOne
@JoinColumn(name = "qualification_id", referencedColumnName = "id")
private Qualification qualification;

@MapsId(value = "institute_id")
@ManyToOne
@JoinColumn(name = "institute_id", referencedColumnName = "id")
private Institute institute;
}

@Embeddable
public class QualificationInstitutePK
{
@Column(name = "qualification_id")
private Long qualification_id;

@Column(name = "institute_id")
private Long institute_id;
}

关于mysql - 使用 JPA 将连接表映射到 Java 中的 Map 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654743/

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