Hello!
I have a problem with my many-to-many relation (bidirectional) with Hibernate. I read in the documentation that only the mapping owner can persist data:
http://docs.jboss.org/hibernate/core/3. ... irectionalQuote:
Changes made only to the inverse end of the association are not persisted. This means that Hibernate has two representations in memory for every bidirectional association: one link from A to B and another link from B to A.
So I have a project and a person with a n:m relation (project is the mapping owner).
So I have a JUnit test method which should remove a person. But the person can not be removed, because there is a connection to a project in the join table project_person.
The remove method in my DAO implementation:
Code:
public void removePerson(int personId){
Person p = findPersonById(personId);
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session sess = sessionFactory.getCurrentSession();
Transaction tx = sess.beginTransaction();
sess.delete(p);
sess.flush();
tx.commit();
}
Error:
Quote:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`testdb`.`project_person`, CONSTRAINT `FKC192077B478777F2` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`))
So what should I do to solve this problem?
I am using a "service interface -> service implementation -> dao interface -> dao implementation" architecture.
Thank you in advance & Best Regards.