微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决一对多关系引发列“ 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。