-->
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: how to get cascade deletion working?
PostPosted: Mon Mar 18, 2013 5:11 am 
Newbie

Joined: Thu Mar 12, 2009 2:49 pm
Posts: 9
Location: Vienna
Okey, so I'm currently trying to figure out how to get cascade deletion working.

Here are my annotated JPA Classes:

Code:
public class Entity {
   @OneToMany(mappedBy = "vistedEntity", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
   @OnDelete(action = OnDeleteAction.CASCADE)
   private Set<Visit> entityVisits;
...
}

public class Visit extends Entity {
   @ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
   @OnDelete(action = OnDeleteAction.CASCADE)
   private Entity vistedEntity;
...
}

public class DatabaseWorker {
public void deleteEntity(Entity entity) {
   EntityManager em = produceEntityManager();
   EntityTransaction tx = em.getTransaction();
   tx.begin();
   entity = em.merge(entity);
   em.remove(entity);
   tx.commit();
   em.close();
}
...
}


And now I want that Hiberate will successfully delete Entity instances, for which Visits exist. But instead my database throws an Exception because of a constraint violations.

My favorite solution would be for hibernate to just modify the database table and add the "ON DELETE CASCADE", which should have happened by adding the @OnDelete annotation from what I've read, but sadly it doesn't.
My usecase doesn't allow me to do this manually, I need the tables to be created and managed by hibernate.


Top
 Profile  
 
 Post subject: Re: how to get cascade deletion working?
PostPosted: Mon Mar 18, 2013 6:27 am 
Newbie

Joined: Thu Mar 12, 2009 2:49 pm
Posts: 9
Location: Vienna
okey, update: after deleting the tables and letting them be recreated by hibernate the "on delete cascade" constrain was defined the right way and it works now. So the problem I was experiencing is that if the table exists, the annotation @OnDelete(action = OnDeleteAction.CASCADE) is ignored instead of just added if not there already.

I've tried this on these 3 versions, all of those don't update the table with the on delete cascade option, they only add it when the table doesn't exist yet:
4.2.0.CR2, 4.1.10.Final, 3.6.10.Final


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.