gpt4 book ai didi

java - Hibernate OneToMany 关联表字段对于repository.save(entityModel) 不能为空

转载 作者:行者123 更新时间:2023-12-02 04:18:57 24 4
gpt4 key购买 nike

有两个实体Policy和PolicyDetails。 Policy.id 与PolicyDetails.policyId 一对多关联。喜欢

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

当我获取数据时一切正常。但我运行policyRepository.save(policy)抛出以下错误-

Hibernate:更新策略集category_id=?、category_name=?、client_id=?、summary=?哪里 id=?Hibernate:更新policy_details设置policy_id=null,其中policy_id=?和id=?Hibernate:更新policy_details设置policy_id=null,其中policy_id=?和id=?

由以下原因引起:java.sql.BatchUpdateException:列“policy_id”不能为空引起原因:java.sql.SQLIntegrityConstraintViolationException:列“policy_id”不能为空

@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;
}

最佳答案

你非常接近,但是,你的映射有点错误。在hibernate中,我们必须有用于映射的对象(而不是id)。为了可用性和速度,建议这种关系是双向的。在某些情况下,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;

因此,将您的 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;
}

关于java - Hibernate OneToMany 关联表字段对于repository.save(entityModel) 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56649108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com