How to remove entries from shared ones that have many, many relationships in jpa

I get a constraint violation exception by deleting entries

I have TransportationEvent relationship tables and conclusion

attitude is like

@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 the database, I have two more tables like

Conclusion_TransportationEvent and TransportationEvent_Conclusion

here is my requirement - I need to delete entries in both tables (TransportationEvent and conclusion)

here I am trying to delete the conclusion table entries, for example below:

 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 get a constraint violation error.

Called: java.sql.SQLException: the DELETE statement was contrary to the REFERENCE constraint "FK30CDAB072AAE439". The conflict occurred in the database "ECT", the table "dbo.TransportationEvent_Conclusion", in the column "outputs_id"

by browsing some forums, I got syntax like

 @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 

and i applied it like

 @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 objects although iam gets the same restriction of restrictions

can someone help me exactly how I need to use this to remove entries from the Conclusion and TransportationEvent.

Thanks in advance!

+4
source share
1 answer

You identified the problem, but did not fix it: you should not have two different connection tables in order to maintain the relationship between TransportEvent and the Summary. You must have one.

You did not define a single, bidirectional ManyToMany association, but two separate unidirectional ManyToMany associations. In bidirectional communication, one side should always be the other side of the other side, which is the owner's side. The mappedBy side must have the mappedBy attribute. So, if you selected MoveEvent as the owner side, the display 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 create a disppear association, you just have to remove the output from the event output list. It is also better to remove the event from the list of conclusion events, have a consistent graphic object, but Hibernate does not care about the back side. Only about the ownerโ€™s side.

Once the output is removed from its event, it no longer refers, and you can remove it from the database.

+6
source

Source: https://habr.com/ru/post/1482146/


All Articles