简体   繁体   中英

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

Can we use java.util.List in one entity and java.util.Set in related entity for ManyToMany relationship? For example:

@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;
  ...
}

Yes, you can. Why don't you simply try it?

A project might want to have employees in a specific order, whereas an employee might just want to have a set of projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM