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