Существует две сущности 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 = ?, имя_категории = ?, 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;
}