I use SpringDataJpa and have entity ValidationResults which has embeddedId of class ValidationResultsIdentity. In this key there is one auto generated field. ValidationResults has a set of ValidationViolation in @OneToMany mapping, which has JoinColumns of embedded key columns.
And I have an issue that when I create new ValidationResults with a ValidationResultsIdentity key without this auto generated field, and save the object. Jpa inserts right values with genreated field in ValidationResults table but it insert null as this field in corresponding ValidationViolation table. To make it more clearly. Here I got insert_id as nulls in ValidationViolation table:
How to repair that ?
final ValidationResults validationResults = new ValidationResults(new ValidationResultsIdentity("Workflow_id", processingDate));
final Set<ValidationViolation> violationsSet = new HashSet<>();
final ValidationViolation validationViolation = new ValidationViolation();
validationViolation.setValidationResults(validationResults);
violationsSet.add(validationViolation);
validationResults.setViolations(violationsSet);
validationResultsRepo.save(validationResults);
//inserts in validation_violation table insert_id as null
Here are my entities
@Embeddable
public class ValidationResultsIdentity implements Serializable {
@Column(name = "workflow_id")
private String workflowId;
@Column(name = "processing_date")
private Date processingDate;
@Column(name = "insert_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long insertId;
public ValidationResultsIdentity(String workflowId, Date processingDate)
{
this.workflowId = workflowId;
this.processingDate = processingDate;
}
....
ValidationResults
@Entity
@Table(name = "validation_results")
public class ValidationResults {
@EmbeddedId
private ValidationResultsIdentity validationResultsIdentity;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "validationResults", cascade = CascadeType.ALL)
private Set<ValidationViolation> violations = new HashSet<>();
....
ValidationViolation
@Entity
@Table(name = "validation_violation")
public class ValidationViolation {
@Id
@Column(name = "violation_id")
@GeneratedValue(strategy = GenerationType.AUTO)
@ApiModelProperty(hidden = true)
private long violationId;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "workflow_id", referencedColumnName = "workflow_id"),
@JoinColumn(name = "processing_date", referencedColumnName = "processing_date"),
@JoinColumn(name = "insert_id", referencedColumnName = "insert_id"),
})
private ValidationResults validationResults;
.....