一对多关系引发列“ permission_group_id”中的空值违反了非空约束

问题描述

Parent.java:

@Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "pgroup_generator")
    @SequenceGenerator(name="pgroup_generator",sequenceName = "pg_seq",allocationSize=50)
    @Column(
               name = "group_id",unique = true,updatable = false,nullable = false
            )
    private Long id;

    
    @Column(name="group_name",unique = true)
    private String groupName;
    
    @OnetoMany(targetEntity=PermissionsEntity.class,mappedBy = "permissionGroup",cascade=CascadeType.ALL,fetch = FetchType.LAZY)
    private List<PermissionsEntity> permissions= new ArrayList<>();

public void setPermissions(List<PermissionsEntity> permissions) {
        
        this.permissions = permissions;
        for(PermissionsEntity p:permissions) {
            p.setPermissionGroup(this);
        }
    }

child.java:

@ManyToOne(fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name="group_id",referencedColumnName = "group_id",insertable = false,updatable = false)
    private PermissionGroupEntity permissionGroup;

以下是错误日志:

 org.postgresql.util.PsqlException: ERROR: null value in column "permission_group_id" violates not-null constraint
  Detail: Failing row contains (11,null,2020-11-02 10:52:34.849,Allow the user to create findings,create audit or workpaper findings,null).
    

解决方法

由于在该insertable = false上有permissionGroup,因此没有被插入,将其保留为空。删除该设置,将其保留为默认true