Java Springboot JPA ManytoMany三个实体之间的Map

wyyhbhjk  于 2023-08-04  发布在  Spring
关注(0)|答案(1)|浏览(137)

我有3个独立的实体在我的项目。流程、客户、代理。本项目是一个过程管理项目。我必须在这三个实体之间建立关系。我想使用JPAMap。流程与客户和代理都有多对多的双向关系。客户和代理都具有双向的多对多的流程关系。我应该在它们之间使用什么样的Map。当删除一个过程时,如果一个客户与该过程相关联,则不应删除该客户实体,因为该客户可能与另一个过程相关联。类似地,对于处理代理关系。以下是三个实体。class Process {

private UUID id;

private String name;

private String description;

private String deepLinkId;

private StatusDto status;

private TaskDto task;

private Set<CustomerEntity> customers;

private Set<AgentEntity> agents;

字符串
} public class Testname {

private UUID id;
@Setter(AccessLevel.NONE)
private int roleId = 1;
private String name;
private UUID notificationId;
private String mobile;
private String email;
private Boolean active;
private Date iat;
private CountryEntity country;
private Set<ProcessEntity> processes;


} public class Uncategorized {

private UUID id;
@Setter(AccessLevel.NONE)
private int role_id = 2;
private String name;
private String mobile;
private String email;
private String companyName;
private Boolean active;
private String username;
private String password;
private Date iat;
private CompanyEntity company;
private Set<ProcessEntity> processes;


}
任何帮助都将不胜感激。

mnemlml8

mnemlml81#

你得先决定主人。在双向多对多关系中,必须将一个实体设置为所有者。在你的例子中,如果你想删除Process实体和相应的customer实体,那么你必须将Process实体设置为所有者。
您必须编写以下代码以将所需实体设置为所有者:

@ManyToMany(mappedBy = "process",CascadeType.PERSIST)
private Set<CustomerEntity> customers;
    
@ManyToMany(mappedBy = "process",CascadeType.PERSIST)
private Set<AgentEntity> agents;
    
@ManyToMany(mappedBy = "customer",CascadeType.PERSIST)
private Set<ProcessEntity> agents;

字符串
数据将像这样存储:

process1 - >  custormer1   // stored as a new record in table
process1 - >  customer 2   // stored as a new record in table

customer1 ->  process 2     // stored as a new record in table
customer2 ->  process 2    // stored as a new record in table


如果删除process1,那么只有相应的行将从表中删除,如果customer1与另一个进程相关联,那么它将存储为不同的记录,因此不会被删除。

相关问题