更新部分字段网上给的方法大都是将原来的数据补上,感觉太麻烦了,有一次修改字段名发现MySQL中原来的字段没被删掉,于是就想能不能多个实体对应同一张表,这样它没有get方法连null都取不出来看它怎么更新

然后我就尝试了一下:

这是原来的数据:

Role.java

package kim.nzxy.auth.models.role;

import kim.nzxy.auth.models.permission.Permission;
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

/**
 * @author xy
 */
@Data
@Entity
public class Role implements Serializable {

    @Id
    private Integer id;
    private String  name;
    private String  remark;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "role_permission",
               joinColumns = {@JoinColumn(name = "role",
                                          referencedColumnName = "id")},
               inverseJoinColumns = {@JoinColumn(name = "permission",
                                                 referencedColumnName = "id")})
    private Set<Permission> permissions;
}

这是RoleSimple.java:

package kim.nzxy.auth.models.role;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author Xiaoyan
 * @date 2019/12/13 9:54
 */
@Entity
@Table(name = "role")
@Data
public class RoleSimple {
    @Id
    private Integer id;
    private String  name;
    private String  remark;
}

这样重新写一个RoleSimpleRepository发现更新时没将关联表中的信息删除,甚是开心,缺点就是多写了一个实体类,谁有更简单的方法请多指教

12-24 10:33