-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ConstraintViolationException with a many-to-many relation
PostPosted: Tue Oct 12, 2010 12:26 pm 
Newbie

Joined: Tue Jun 01, 2010 10:18 am
Posts: 15
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. ... irectional

Quote:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.