[英]About Mapping between entities in spring-data
在使用spring-data和hibernate进行spring-boot的过程中,我有一个实体AppUser(它可以是Client或A devlopper,它取决于每个角色,所以我有另一个实体角色..)
和实体干预。
AppUser(当他具有客户端角色时)可以添加/删除one_to_many干预。
AppUser(具有角色开发者权限时)可以由客户干预的一对多(他可以验证或不验证干预)。
因此,由于我只有一个实体AppUser定义如下:
@Entity
@Data
@AllArgsConstructor @NoArgsConstructor
public class AppUser implements Serializable {
@Id @GeneratedValue
private Long id;
@Column(unique = true)
private String username;
private String password;
private String prenom;
private String nom;
private Long tel;
private String cin;
private String email ;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<AppRole> roles = new ArrayList<>();
@OneToMany(mappedBy = "appUser" )
@Fetch(value = FetchMode.SUBSELECT)
@JsonManagedReference(value="appuser-intervention")
private Collection<Intervention> interventions = new
ArrayList<Intervention>();
public void addToInterventions(Intervention intervention){this.interventions.add(intervention);}
public void addToRoles(AppRole role){this.roles.add(role);}
}
实体干预:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Intervention implements Serializable {
@Id @GeneratedValue
private Long Id;
private String objet;
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date date;
@Column(columnDefinition="BOOLEAN DEFAULT false")
private boolean valid;
@ManyToOne
@JoinColumn(name = "Id_AppUser")
@JsonBackReference(value="appuser-intervention")
private AppUser appUser;
}
因此,在AppUser(Client,Devlopper)与干预之间存在on_to_many
我是否需要在appUser中添加两个干预列表?
并且我还需要在实体干预中添加两个对象AppUser吗? (一个用于客户,一个用于开发者)?
还是只有AppUser中的一个列表和干预中的一个对象?
等待您的帮助:)。
编辑
@ManyToOne
@JoinColumn(name = "Id_AppUser")
@JsonBackReference(value="appuser-intervention")
private AppUser appClient;
@ManyToOne
@JoinColumn(name ="Id_AppUser")
@JsonBackReference("appuser-intervention")
private AppUser appDevlopper ;
您无需为开发人员或客户端的每种用户类型定义两个列表。 而是声明两个列表,您可以在AppUser中提供role / userType。
从我的角度来看,有两种方法:
为开发人员和客户端创建不同的类(您仍然可以将它们保留在一个db表中-只需在hibernate中配置继承)
您可以在AppUser内保留一个列表,并在Intervention中有2个AppUser链接-一个链接到验证干预的开发人员,另一个链接到进行干预的客户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.