jpa 使用软删除Hibernate进行多次更新

n9vozmp4  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(178)

尝试使用@SQLDelete注解实现软删除。角色(1)和项目组(N)具有一对多关系。删除角色时,我必须断开关系。删除角色时,角色的RECORD_STATUS设置为D,这是正确的,但项目组仍与角色具有关系。我们尝试了;

@SQLDelete(sql = "UPDATE PROJECT_GROUP SET ROLE_OBJID = NULL where ROLE_OBJID = ?; " +
    "UPDATE PROJECT_GROUP SET RECORD_STATUS = 'D' where OBJID = ?")

但没有成功。
角色实体;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
@Table(name = "ROLE")
@EntityListeners(AuditingEntityListener.class)
@DynamicUpdate
@EqualsAndHashCode(callSuper = false)
@SQLDelete(sql = "UPDATE ROLE SET RECORD_STATUS = 'D' where OBJID = ?")
@Where(clause = "RECORD_STATUS = 'A'")
public class Role extends AuditableEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
        name = "UUID",
        strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "OBJID", unique = true, nullable = false)
private UUID id;

@Column(name = "NAME", nullable = false, unique = true)
private String name;

@Column(name = "DESCRIPTION", nullable = false)
private String description;

@Enumerated(EnumType.STRING)
@Column(name = "RECORD_STATUS", nullable = false)
@ColumnDefault("A")
private Status status = Status.A;

@ManyToMany(
        fetch = FetchType.LAZY,
        cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
        })
@JoinTable(name = "ROLE_AUTHORIZATION",
        joinColumns = {@JoinColumn(name = "ROLE_OBJID")},
        inverseJoinColumns = {@JoinColumn(name = "AUTHORIZATION_OBJID")})
@Where(clause = "RECORD_STATUS = 'A'")
private Set<Authorization> authorizations = new HashSet<>();

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "role")
@Where(clause = "RECORD_STATUS = 'A'")
private Set<ProjectGroup> projectGroup = new HashSet<>();

public void addAuthorization(Authorization authorization) {
    if (this.authorizations == null) {
        this.authorizations = new HashSet<>();
    }
    this.authorizations.add(authorization);
}

public void removeAuthorizationByAuthorizationId(UUID authorizationId) {
    this.authorizations.stream().filter(auth -> auth.getId().equals(authorizationId)).findFirst().ifPresent(authorization -> this.authorizations.remove(authorization));
}

}
项目组实体;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
@Table(name = "PROJECT_GROUP")
@EntityListeners(AuditingEntityListener.class)
@DynamicUpdate
@EqualsAndHashCode(callSuper = false, exclude = {"project"})
@SQLDelete(sql = "UPDATE PROJECT_GROUP SET RECORD_STATUS = 'D' where OBJID = ?")
@Where(clause = "RECORD_STATUS = 'A'")
public class ProjectGroup extends AuditableEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
        name = "UUID",
        strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "OBJID", unique = true, nullable = false)
private UUID id;

@Column(name = "NAME", nullable = false, unique = true)
private String name;

@OneToOne(fetch = FetchType.LAZY)
@JsonIgnore
@JoinColumn(name = "PROJECT_OBJID", referencedColumnName = "OBJID")
@Where(clause = "RECORD_STATUS = 'A'")
private Project project;

@Enumerated(EnumType.STRING)
@Column(name = "RECORD_STATUS", nullable = false)
@ColumnDefault("A")
private Status status = Status.A;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ROLE_OBJID", referencedColumnName = "OBJID")
private Role role;

public void addProject(Project project) {
    this.project = project;
    project.setProjectGroup(this);
}
}
liwlm1x9

liwlm1x91#

我建议您在Role中使用@PreRemove侦听器,它将每个关联的ProjectGrouprole设置为null。

相关问题