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: Bulk Delete with On Cascade Delete on DB Foreign Key
PostPosted: Tue Jun 23, 2009 6:34 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hello,

I use MySQL 5.0 and Hibernate Core 3.3.1.GA, Hibernate Tools 3.2.4 GA and Hibernate Annotation 3.4.0.

My requirement is to be able to delete an Object tree without navigating it. Therefore I looked in the dml deletes in the documentation. Here is my query:

Code:
int dailyDataDeleted = getSession().createQuery("delete from DailyData as dbd where dbd.day between '2009-06-01' and '2009-06-05').executeUpdate();


My problem is that the DailyData has a bunch of children classes. For each of these children I have setup the associations so that the children are deleted on cascade. For example:

Code:
    private java.util.SortedSet<HourlyData> hoursData = new java.util.TreeSet<HourlyData>(new HourlyDataComparator<HourlyData>());
    @javax.persistence.OneToMany(cascade=javax.persistence.CascadeType.ALL)
    @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @javax.persistence.JoinColumn(name="DAILY_DATA_HOURS_DATA_FK")
    @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.COMPARATOR,comparator=HourlyDataComparator.class)
    public java.util.SortedSet<HourlyData> getHoursData() {
        return this.hoursData;
    }


When I try to delete a DailyData with the normal session method (i.e. session.delete(object)), it works fine. The DailyData is deleted and the underlying HourlyData are also deleted.

On the other hand when I use the bulk delete, it doesn't work with the exception that there is a reference constraint.

I have read many posts and what I understood is that the ON CASCADE DELETE condition must be at the database level. I checked my database and the conditions were not there (the on delete is at 'restrict'). I then tried to update manually the database by dropping my foreign keys and recreating them to add the ON CASCADE DELETE conditions. When I did so, my bulk delete completed correctly.

My question is the following: How can I get hbm2ddl to add those constraints for me? I have tried modifying the dialect with the following option but to no avail:

Code:
public class MyMySQL5Dialect extends MySQL5Dialect {
    public MyMySQL5Dialect() {
        super();
    }
   
    public boolean supportsCascadeDelete(){
        return true;
     }
}


Any help understanding how I can generate the on cascade constraint would be greatly appreciated.

Thank you,

Francois


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.