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.  [ 2 posts ] 
Author Message
 Post subject: Question about Cascading Deletes
PostPosted: Fri Apr 10, 2009 4:25 pm 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
Hibernate version: 3.2.5.ga

Mapping documents: annotations

Code between sessionFactory.openSession() and session.close(): Using HibernateDaoSupport

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 5.0

The generated SQL (show_sql=true): true

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

Hello. I am new to hibernate.

I have an entity called Company with a link to Audit. When I delete a Company I want all the Audits deleted as well. Here is how Company is defined.

Code:
@Entity
@NamedQueries({
   @NamedQuery(
         name="CompanyByName",
         query="from Company l where l.name = :name")
})
public class Company implements Serializable {
   
   @OneToMany(cascade=CascadeType.ALL)
   private List<Audit> audits = new ArrayList<Audit>();


Here is how Audit looks.

Code:
@Entity
@NamedQueries({
   @NamedQuery(
         name="AuditByCompany",
         query="from Audit l where l.company = :company")
})
public class Audit implements Serializable {
   
   private static final long serialVersionUID = -220980587408360814L;

   @ManyToOne
   private Company company;


I generate the schema using mvn hibernate3:hbm2ddl. I am able to save things fine. However when I try and delete a Company I get the following error:

Code:
//here is the code that deletes, in a method that is part of a class that extends HibernateDaoSupport
public void delete(Company c) {
      getHibernateTemplate().delete(c);
}

Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`data`.`audit`, CONSTRAINT `FK3CAABBB4C674D5C` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`))


When I check out a table I can see there is a foreign key constraint.

However I thought cascade=CascadeType.ALL would handle this. What am I not understanding?

Thanks,

Luke


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 9:19 pm 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
If I'm not mistaken, you have a circular dependency here. Try adding a "mappedBy" on the OneToMany side.

http://josian.wordpress.com/2006/09/09/ ... e-to-many/
http://www.hibernate.org/hib_docs/annot ... ntity.html


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

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.