简体   繁体   English

对于repository.save(entityModel),Hibernate OneToMany关联表字段不能为null

[英]Hibernate OneToMany associated table field cannot be null for repository.save(entityModel)

There are two entity Policy and PolicyDetails. 有两个实体Policy和PolicyDetails。 Policy.id is one to many associated with PolicyDetails.policyId. Policy.id是与PolicyDetails.policyId相关联的一对多。 like 喜欢

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "policy_id", referencedColumnName = "id")
private List<PolicyDetails> policyDetails;

When i GET the data all are ok. 当我获取数据时一切正常。 But i run policyRepository.save(policy) throw below error- 但我运行policyRepository.save(policy)抛出以下错误 -

Hibernate: update policy set category_id=?, category_name=?, client_id=?, summary=? Hibernate:更新策略集category_id =?,category_name =?,client_id = ?, summary =? where id=? 其中id =? Hibernate: update policy_details set policy_id=null where policy_id=? Hibernate:update policy_details set policy_id = null其中policy_id =? and id=? 和id =? Hibernate: update policy_details set policy_id=null where policy_id=? Hibernate:update policy_details set policy_id = null其中policy_id =? and id=? 和id =?

Caused by: java.sql.BatchUpdateException: Column 'policy_id' cannot be null Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'policy_id' cannot be null 引起:java.sql.BatchUpdateException:列'policy_id'不能为null引起:java.sql.SQLIntegrityConstraintViolationException:列'policy_id'不能为null

@Entity
@Data
@Table(name = DBTables.POLICY)
public class Policy implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer Id;

    @Column(name = "summary")
    @NotNull(message = ErrorCode.SUMMARY_NOT_FOUND)
    private String summary;

    @Column(name = "category_id")
    @NotNull(message = ErrorCode.CATEGORY_ID_NOT_FOUND)
    private Integer categoryId;

    @Column(name = "category_name")
    @NotNull(message = ErrorCode.CATEGORY_NAME_NOT_FOUND)
    private String categoryName;

    @Column(name = "client_id")
    @NotNull(message = ErrorCode.CLIENT_ID_NOT_FOUND)
    private Integer clientId;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "policy_id", referencedColumnName = "id")
    private List<PolicyDetails> policyDetails;
}
@Entity
@Data
@Table(name = DBTables.POLICY_DETAILS)
public class PolicyDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    @Column(name = "policy_id")
    @NotNull(message = ErrorCode.POLICY_ID_NOT_FOUND)
    private Integer policyId;

    @Column(name = "period")
    @NotNull(message = ErrorCode.PERIOD_NOT_FOUND)
    private String period;

    @Column(name = "logic_id")
    @NotNull(message = ErrorCode.LOGIC_ID_NOT_FOUND)
    private Integer logicId;

    @Column(name = "logic_value")
    @NotNull(message = ErrorCode.LOGIC_VALUE_NOT_FOUND)
    private Integer logicValue;

    @Column(name = "policy_type_id")
    @NotNull(message = ErrorCode.POLICY_TYPE_ID_NOT_FOUND)
    private Integer policyTypeId;
}
    public String saveRegisterPolicy(PolicyRequestDto policyRequestDto, Integer userId) {

        Policy policy = new Policy();

        policy.setId(policyRequestDto.getId());
        policy.setSummary(policyRequestDto.getSummary());
        policy.setCategoryId(policyRequestDto.getCategoryId());
        policy.setCategoryName(policyRequestDto.getCategoryName());
        policy.setClientId(userId);

        List<PolicyDetails> policyDetails = new ArrayList<>();
        policyDetails.addAll(policyRequestDto.getCancelPolicies());
        policyDetails.addAll(policyRequestDto.getDelayPolicies());

        policyDetails.forEach(eachPolicyDetails ->
            eachPolicyDetails.setPolicyId(policyRequestDto.getId())
        );

        policy.setPolicyDetails(policyDetails);

        policyRepository.save(policy);


        return null;
    }

You are very close, however, your mapping is a little bit wrong. 你非常接近,但你的映射有点不对劲。 In hibernate, we must have the object (not an id) for the mapping. 在hibernate中,我们必须拥有映射的对象(不是id)。 And the relationship is recommended to be bidirectional, for both: usability and speed. 建议双向关系:可用性和速度。 Hibernate will create awkward queries with the unidirectional many-to-one and one-to-many relationships in some cases. 在某些情况下,Hibernate将使用单向多对一和一对多关系创建尴尬的查询。

    @NotNull(message = ErrorCode.POLICY_ID_NOT_FOUND)
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "policy_id", referencedColumnName = "id", nullable = false)
    private Policy policy;

So change your PolicyDetails to be: 因此,将PolicyDetails更改为:

@Entity
@Data
@Table(name = DBTables.POLICY_DETAILS)
public class PolicyDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    @NotNull(message = ErrorCode.POLICY_ID_NOT_FOUND)
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "policy_id", referencedColumnName = "id", nullable = false)
    private Policy policy;

    @Column(name = "period")
    @NotNull(message = ErrorCode.PERIOD_NOT_FOUND)
    private String period;

    @Column(name = "logic_id")
    @NotNull(message = ErrorCode.LOGIC_ID_NOT_FOUND)
    private Integer logicId;

    @Column(name = "logic_value")
    @NotNull(message = ErrorCode.LOGIC_VALUE_NOT_FOUND)
    private Integer logicValue;

    @Column(name = "policy_type_id")
    @NotNull(message = ErrorCode.POLICY_TYPE_ID_NOT_FOUND)
    private Integer policyTypeId;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠,repository.save-java.lang.NullPointerException:null-当对象存在时 - Hibernate, repository.save - java.lang.NullPointerException: null - while Object exist Hibernate Validator未在单元测试中调用Spring Repository.save()时触发 - Hibernate Validator not triggered as Spring Repository.save() is invoked in unit tests Hibernate 5 Java双向oneToMany字段为空,但表包含数据 - Hibernate 5 Java bidirectional oneToMany field is null but table contains data 重试 repository.save() 时出现 StaleObjectStateException - StaleObjectStateException when retrying repository.save() 如何访问从 Hibernate AbstractEvents 和 EventListeners 调用 repository.save(entity) 时使用的原始实体? - How to access the original Entity used when repository.save(entity) is invoked from Hibernate AbstractEvents and EventListeners? java.sql.SQLException:不正确的 integer 值:在 varchar 字段上同时执行 ZF0B4A299C4517149Aave() - java.sql.SQLException: Incorrect integer value: on varchar field while doing jpa repository.save() repository.save function 更改其子实体的 id - repository.save function changes id of it's children entity 每次我执行 repository.save() 时,LocalDateTime 都会更新 4 小时 - LocalDateTime getting updated by 4 hours everyTime I do repository.save() Spring Boot Repository.save()方法不起作用-没有提交 - Spring Boot Repository.save() methods do not work - No commit Hibernate 双向@OneToMany 映射返回列 'xxx' 不能为 null - Hibernate bidirectional @OneToMany mapping return Column 'xxx' cannot be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM