关于spring-data

时间:2018-06-07 12:09:13

标签: java spring spring-boot spring-data

使用spring-data和hibernate在spring-boot中工作,我有一个实体AppUser(它可以是一个客户端或一个devlopper,它依赖于每个角色,所以我有另一个实体角色..)

实体干预。

AppUser(当他有角色客户端时)他可以添加/删除one_to_many干预。

AppUser(当他有角色devlopper时)他可以占用客户干预的一半(他可以验证或不干预)。

因为我只有一个实体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(客户端,Devlopper)与干预之间存在on_to_many

我需要在appUser中添加两个干预列表吗?

而且我还需要在实体干预中添加两个对象AppUser吗? (一个用于客户端,一个用于devlopper)?

或AppUser中只有一个列表和干预中的一个对象?

等待你的帮助:)。

修改

            @ManyToOne
            @JoinColumn(name = "Id_AppUser")
            @JsonBackReference(value="appuser-intervention")
            private AppUser appClient;

            @ManyToOne
            @JoinColumn(name ="Id_AppUser")
            @JsonBackReference("appuser-intervention")
            private AppUser appDevlopper ;

2 个答案:

答案 0 :(得分:0)

您不需要为开发人员或客户端为每种用户类型定义两个列表。而是声明两个列表,您可以在AppUser中提供角色/ userType。

答案 1 :(得分:0)

从我的观点来看,有两种方式:

  1. 为开发人员和客户端创建不同的类(您仍然可以将它们保存在一个db表中 - 只需在hibernate中配置继承)

  2. 您可以在AppUser中保留一个列表,并在干预中有2个AppUser链接 - 一个用于验证干预的开发人员,以及制作干预的客户。