Reputation: 69
I am getting the constraint violation exception whil deleting the entries
I have Relation tables TransportationEvent and Conclusion
relation islike
@Entity
public class TransportationEvent {
...
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
private List<Conclusion> conclusions = new ArrayList<Conclusion>();
...
}
@Entity
public class Conclusion {
....
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private List<TransportationEvent> transportationEvents = new ArrayList<TransportationEvent>();
....
}
here in database i have got another two table like
Conclusion_TransportationEvent and TransportationEvent_Conclusion
here my requirement is i need to delete records in both tables (TransportationEvent and Conclusion)
here i am trying to delete conclusion table records like bellow:
removeConclusions(conclusion.getId());
public void removeConclusions(Long id) {
entityManager = dbConn.getConnection();
Conclusion conclusion = entityManager.find(Conclusion.class, id);
entityManager.getTransaction().begin();
entityManager.remove(conclusion);
entityManager.getTransaction().commit();
}
but i am getting constraint violation error .
Caused by: java.sql.SQLException: The DELETE statement conflicted with the REFERENCE constraint "FK30CDAB072AAE439". The conflict occurred in database "ECT", table "dbo.TransportationEvent_Conclusion", column 'conclusions_id'
by searching some forums i got syntax like
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
and i applied that as
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<Conclusion> conclusions = new ArrayList<Conclusion>();
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<TransportationEvent> transportationEvents = new ArrayList<TransportationEvent>();
in both entities even though iam geting same constraint violation
can some one help me how exactly i need to use this to delete the records from Conclusion and TransportationEvent.
Thanks in advance!
Upvotes: 4
Views: 7098
Reputation: 691635
You identified the problem, but didn't fix it: you shouldn't have two different join tables to hold the association between a TransportationEvent and a Conclusion. You should have a single one.
You haven't defined a single, bidirectional ManyToMany association, but two separate unidirectional ManyToMany associations. In a bidirectional association, one side must always be the inverse side of the other side, which is the owner side. The inverse side must have the mappedBy
attribute. So, if you choose TransportationEvent as the owner side, the mapping should be:
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
private List<Conclusion> conclusions = new ArrayList<Conclusion>();
and
@ManyToMany(mappedBy = "conclusions" fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private List<TransportationEvent> transportationEvents = new ArrayList<TransportationEvent>();
And now, to make an association disppear you must simply remove the conclusion from the list of conclusions of the event. It's better to also remove the event from the list of events of the conclusion, to have a coherent object graph, but Hibernate doesn't care about the inverse side. Only about the owner side.
Once the conclusion is removed from its event, it's not referenced anymore, and you can remove it from the database.
Upvotes: 6