Reputation: 658
i am trying to delete a row from my child entity . Loan is my parent class and DVLoanParticipants is child class. My Loan Object is looking like this to run delete .
Loans(loanId=196777, dvLoanParticipantsMap={})
My Expectation is to delete the row from DVLoanParticipants , but hibernate trying to run an update statement and failing.
@Entity
public class Loans {
@Id
@Column(name = "LOAN_ID")
private Long loanId;
@OneToMany(targetEntity = DVLoanParticipants.class, cascade =
CascadeType.ALL,orphanRemoval=true )
@JoinColumn(name = "LOAN_ID")
@MapKey(name = "dvLoanParticipantsId.dvpParticipantName")
@LazyCollection(LazyCollectionOption.FALSE)
private Map<String, DVLoanParticipants> dvLoanParticipantsMap;
}
@Entity
public class DVLoanParticipants implements Serializable{
private static final long serialVersionUID = 1L;
@EmbeddedId
private DVLoanParticipantsId dvLoanParticipantsId;
}
@Embeddable
public class DVLoanParticipantsId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "LOAN_ID")
private Long loanId;
@Column(name = "DVP_PARTICIPANT_NAME")
private String dvpParticipantName;
}
The Update Statement is trying to execute is
update DV_LOAN_PARTICIPANTS set LOAN_ID=null where LOAN_ID=?
But why its running Update statement ? what i can do to run the delete statement?
Upvotes: 1
Views: 2387
Reputation: 2086
CascadeType.ALL
(specifically CascadeType.REMOVE
here) doesn't mean the children will be removed if removed from the collection. It means that every state transition of the parent is cascaded to his child. Children are only removed automatically when the parent is deleted.
So by deleting the DVLoanParticipants
in the collection, you are not asking Hibernate to delete the entity. You are asking him to update the record to point to no Loans
, which results in a SQL update with NULL
in the foreign key.
You need to had @OneToMany(orphanRemoval=true)
for Hibernate to remove a child when he is separated from his parents (it must be detached from all parents for it to work).
Upvotes: 1