-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cascade problem
PostPosted: Mon Jul 14, 2008 6:56 am 
Newbie

Joined: Mon Jul 14, 2008 6:48 am
Posts: 4
Hi,
I have one requirement in my project which is that I need to set value of Cascade to "none" when I save the object to the database and I need to set the value of this attribute to "All" when I load the object from the database for the same class. If I do this then I am able to avoid the duplications in the database. So could anybody tell me how to achieve this functionality for Cascade?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 10:17 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You know, I think you might be approaching the problem from the wrong angle.

Why are you getting 'duplicates.' You shouldn't be. The real problem is why is that happening. I think you're trying to work around hibernate, rather than really taking advantage of it, which will cause more problems later down the road.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Cascade problem
PostPosted: Tue Jul 15, 2008 12:09 am 
Newbie

Joined: Mon Jul 14, 2008 6:48 am
Posts: 4
Hi Cameron,
I will tell you the details about mjy code.

I have two classes:
public class A{
private int id;
private B b;

//and getters and setters for all attributes.

}

public class B{
int id;
//Other attributes and all getter setters
}

Now there are two tables for Class suppose AT and for class B BT.
Table BT contains the static data suppose 8 rows which do not change. Now in table AT for attribute a I want the reference i.e. foreign key from Table BT. Hibernate will do this association using mapping file.

Now consider the following senario:

I create the instance of Class A and saveOrUpdate it using session.saveOrUpdate() method. If the cascades value is "all" then when saving this new instance it will create new id's for Class A as well as B and associate the classes A and B using new ids. So even if the values of columns other than id don't change in Class B the hibernate is going to save this as new instance in the table BT. So this creates the duplications of rows in the database. To avoid this I want the value of cascade ="none" when I will save the instance and it should be "all" when I load the instance of class A so that all associations will be taken care by hibernate.

I think you have understood my problem. If you think I am wrong somewhere with hibernate's persistence logic please clarify me.


Last edited by nkasat on Tue Jul 15, 2008 12:15 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Cascade problem
PostPosted: Tue Jul 15, 2008 12:09 am 
Newbie

Joined: Mon Jul 14, 2008 6:48 am
Posts: 4
Also what reference would you suggest to me for studying persistence logic used by hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 8:36 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This JavaRanch thread seems to provide a bit more insight on the topic:

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=004209

As far as learning Hibernate, I'd always recommend my own book, Hibernate Made Easy, but I'm biased. :)

Hibernate Made Easy will get you started and up and running very quickly, and you will learn how Hibernate works, and why various things happen when you're working with Hibernate and JPA. But Hibernate Made Easy isn't the reference. You also need Java Persistence with Hibernate by Gaving King by your side at all times. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Cascade problem
PostPosted: Thu Jul 17, 2008 12:09 am 
Newbie

Joined: Mon Jul 14, 2008 6:48 am
Posts: 4
Thanks very much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.