I'm using Hibernate version 6.2.4.Final. I have an entity called "Batch":
@Audited
@Entity
@Table(name = "batch")
public class Batch extends BaseEntity implements Serializable {
@OneToMany(mappedBy = "batch", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<BatchTechnicalRecord> technicalRecords;
...
}
I also have the "BatchTechnicalRecord" entity:
@Audited
@Entity
@Table(name = "batch_technical_record")
public class BatchTechnicalRecord extends BaseEntity implements Serializable {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "batch_technical_record_attachment", joinColumns = @JoinColumn(name = "id_batch_technical_record"), inverseJoinColumns = @JoinColumn(name = "id_attachment"))
private List<Attachment> attachments;
...
}
And finally, the "BatchTechnicalRecordAttachment" entity that relates "Attachment" to "BatchTechnicalRecord":
@Audited
@Entity
@Table(name = "batch_technical_record_attachment")
public class BatchTechnicalRecordAttachment extends BaseEntity implements Serializable {
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id_attachment", referencedColumnName = "id", nullable = false, foreignKey = @ForeignKey(name = "fk_batch_technical_record_attachment_attachment"))
private Attachment attachment;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_batch_technical_record", referencedColumnName = "id", nullable = false, foreignKey = @ForeignKey(name = "fk_fk_batch_technical_record_attachment_attachment_batch_technical_record"))
private BatchTechnicalRecord batchTechnicalRecord;
...
}
When I try to remove the "Batch", the cascade should remove the associated "BatchTechnicalRecord" entities along with their "BatchTechnicalRecordAttachment" and "Attachment" entities. However, Hibernate is attempting to insert a null ID in the "batch_technical_record_attachment_aud" table for some reason:
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" of relation "batch_technical_record_attachment_aud" violates not-null constraint
Detail: Failing row contains (null, 2052, 2, null, null, null, null, 15, 35).
Any help would be appreciated in resolving this issue. Thank you.