M
M
MaxLich2019-12-16 12:44:55
Java
MaxLich, 2019-12-16 12:44:55

How to work with a collection of related entities (their modification is not required)?

Hello. There are two entities (password policies and groups):

@Entity
@Table(name = "sys_password_policy", schema = "oauth")
public class SysPasswordPolicyEntity implements Serializable {
    private Long policyId;
    private String policyName;
    private Set<SysGroupEntity> sysGroupsByPolicyId;
// ...

    @OneToMany(mappedBy = "sysPasswordPolicyByPasswordPolicyId", fetch = FetchType.LAZY)
    public Set<SysGroupEntity> getSysGroupsByPolicyId() {
        return sysGroupsByPolicyId;
    }

    public void setSysGroupsByPolicyId(Set<SysGroupEntity> sysGroupsByPolicyId) {
        this.sysGroupsByPolicyId = sysGroupsByPolicyId;
    }
}

@Entity
@Table(name = "sys_group", schema = "oauth")
public class SysGroupEntity implements Serializable {
    private Long groupId;
    private String groupName;
    private SysPasswordPolicyEntity sysPasswordPolicyByPasswordPolicyId;

//...

    @ManyToOne
    @JoinColumn(name = "password_policy_id", referencedColumnName = "policy_id")
    public SysPasswordPolicyEntity getSysPasswordPolicyByPasswordPolicyId() {
        return sysPasswordPolicyByPasswordPolicyId;
    }

    public void setSysPasswordPolicyByPasswordPolicyId(SysPasswordPolicyEntity sysPasswordPolicyByPasswordPolicyId) {
        this.sysPasswordPolicyByPasswordPolicyId = sysPasswordPolicyByPasswordPolicyId;
    }
}

Need to:
  • When creating a policy, it was possible to assign some groups to it (groups should not be created, and no fields should be updated for them, except for sysPasswordPolicyByPasswordPolicyId)
  • When updating the policy, it was possible to remove a group from the list, as well as add it from the list (this does not require groups to be deleted from the database, created in the database, and it is not necessary that all fields be updated in their database, except for sysPasswordPolicyByPasswordPolicyId; it is necessary, so that when removed from the list, the value of the sysPasswordPolicyByPasswordPolicyId field becomes null, when added, this field is assigned a link to the current group)

How are these tasks usually handled? (it seems the situation is standard, and often occurs)
PS I tried to search the Internet, but it’s hard to find something in my situation, but I haven’t found anything on related general topics yet.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question