I am trying to delete a Patient entity without deleting the associated OutboundMessage entities as i want to keep them in the database for reporting/historical purposes. This is the relevant code :
Patient entity
@OneToMany (mappedBy="patient", fetch = FetchType.EAGER,orphanRemoval = false)
public Set<OutboundMessage> getOutboundMessages()
{
return outboundMessages;
}
OutboundMessage entity
@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name = "id_patient")
public Patient getPatient()
{
return patient;
}
When i set a cascade type on the Patient side the records are deleted which is not what i want. When trying as shown in the code above (without the cascade type) I get the following exception :
The DELETE statement conflicted with the REFERENCE constraint "FKqjpga9w6wp3qk26ox9pg252d9". The conflict occurred in database "MDHIS", table "dbo.tblOutboundMessage", column 'id_patient'.
What settings does the owning entity need to allow deletion without cascading to children entities and without cleaning orphaned records?
Thanks!
You need to allow
null
values in your foreign key column (@JoinColumn
) as follows:Then in your DAO you need to set
null
values for all theOutboundMessage
related to thePatient
you're about to delete and only then delete it, as follows: