JPA:删除关系时设置默认值

时间:2018-04-12 12:35:41

标签: java spring hibernate spring-boot jpa

我有一个自己加入的实体权限由父ID加入,我修改整个族树只需修改根,创建和修改树节点效果很好,但是当我删除一个节点时,jpa设置实体' s parent id为null,考虑到数据库性能,我在每一列中都不使用null,因此,它带来了MySQLIntegrityConstraintViolationException: Column' parent_id'不能为空。 SQL是update t_authority set parent_id=null where parent_id=?

所以,我的问题是,为什么设置为0L的默认值parentId不起作用?删除关系时,我该怎么做才能使它在parentId中使用默认值。

@Entity
@Table(name = "t_authority")
@SQLDelete(sql =
        "UPDATE t_authority " +
        "SET status = " + DB_STATUS_NOT_EXISTS +
        " WHERE id = ?")
@Where(clause = "status = " + DB_STATUS_EXISTS)
public class Authority implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "authority_name")
    private String authorityName;

    @Column(name = "authority_Id")
    private String authorityId;

    @Column(name = "parent_id")
    private Long parentId = 0L;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    @Where(clause = "status = " + DB_STATUS_EXISTS)
    private Set<Authority> childAuthorities;

mysql表

create table `t_authority` (
    `id` bigint unsigned not null primary key auto_increment,
    `authority_name` varchar(100) not null default '' comment 'authority name',
    `authority_id` varchar(100) not null default '' comment 'authority id',
    `parent_id` bigint unsigned not null default 0 comment 'parent id'
) engine=innodb default charset=utf8 collate=utf8_general_ci;

0 个答案:

没有答案