[英]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.