-->
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: Difference between Cascade.ALL and separated options
PostPosted: Fri Sep 18, 2009 1:08 pm 
Newbie

Joined: Fri Sep 18, 2009 12:45 pm
Posts: 2
I have Cluster/Shelf relation(parent/child) like this.
Cluster.java
----------
Code:
@OneToMany(cascade = { CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE }, mappedBy = "cluster")
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public List<ShelfDBObj> getShelves() {
  return shelves;
}


Between a start and commit transaction for Cluster/Shelf
Code:
Cluster exists in database
..starttrx
ClusterDBObj cluster = //query cluster
ShelfDBObj shelf = new ShelfDBObj();
cluster.addShelf(shelf);
dao.persist(shelf);    [b](Is Needed to save in database)[/b]
commit trx


I have Shelf/Card relation(parent/child) like this.
Shelf.java
---------
Code:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "shelf")
public List<CardDBObj> getCards() {
  return cards;
}


Between a start and commit transaction for Shelf/Card
Code:
Shelf exists in database
..starttrx
ShelfDBObj shelf = //query shelf
CardDBObj card = new CardDBObj();
shelf.addCard(card);
commit trx // dao.persist(card) is not needed


At the point of commit transaction, the first trx needs an explicit save and the second trx does not need an explicit save. Why? What are the differences in the Cascade types. Please explain.


Top
 Profile  
 
 Post subject: Re: Difference between Cascade.ALL and separated options
PostPosted: Fri Sep 18, 2009 7:33 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi sridhar.signon,

the Hibernate docs state something about cascading in Sec. 10.11 http://docs.jboss.org/hibernate/stable/core/reference/en/html/objectstate.html#objectstate-transitive and there are already some possible hints in Sec. 10.2 http://docs.jboss.org/hibernate/stable/core/reference/en/html/objectstate.html#objectstate-makingpersistent.

So, if you read carefully and put it all together you might figure out that CascadeType.ALL includes save-update. save-update will pass a transient object to save(). Referring to Sec. 10.2 http://docs.jboss.org/hibernate/stable/core/reference/en/html/objectstate.html#objectstate-makingpersistent, save() results in an immediate DB hit whereas persist() might be postponed.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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.