gpt4 book ai didi

java - JPA : Can we use different collection type on either sides of ManyToMany relationship?

转载 作者:行者123 更新时间:2023-12-02 08:04:18 26 4
gpt4 key购买 nike

我们可以在一个实体中使用 java.util.List 并在相关实体中使用 java.util.Set 来建立 ManyToMany 关系吗?例如:

@Entity
public class Employee {

@Id
@Column(name="EMP_ID")
private long id;
...

@ManyToMany
@JoinTable(
name="EMP_PROJ",
joinColumns={@JoinColumn(name="EMP_ID", referencedColumnName="EMP_ID")},
inverseJoinColumns={@JoinColumn(name="PROJ_ID", referencedColumnName="PROJ_ID")})
private Set<Project> projects;
...
}

@Entity
public class Project {

@Id
@Column(name="PROJ_ID")
private long id;
...

@ManyToMany(mappedBy="projects")
private List<Employee> employees;
...
}

最佳答案

是的,可以。你为什么不尝试一下呢?

项目可能希望按特定顺序拥有员工,而员工可能只想拥有一组项目。

关于java - JPA : Can we use different collection type on either sides of ManyToMany relationship?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8414049/

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