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.  [ 4 posts ] 
Author Message
 Post subject: Using Many to One Relationship
PostPosted: Thu Jul 22, 2010 7:49 am 
Newbie

Joined: Thu Mar 19, 2009 4:47 am
Posts: 8
Hi All,

I have gone through one sample at
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-one-1.html

and tested the code which is working fine.

In above code If I want to save a Student Object I will save Address also in database which means that table Student and Address both will be filled with one more row.

My Scenario:
In my case huge Address table is already there and I just need to save Student Object by selecting Address_id (typically from UI). There should be no adding records of Address table.

Please let me know how to do that using Many to One Relationship.

I know it can be done using without using any Relationship.

Please help.

Thanks and Ragards,
Vikash Anand.


Top
 Profile  
 
 Post subject: Re: Using Many to One Relationship
PostPosted: Thu Jul 22, 2010 7:54 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
I think you will only need to remove the cascade="all" to the mapping of the property studentAddress in Student.hbm.xml.
Be carefull, before saving a Student you will have to set his address with one yet present in DB.

Bye
Stefano


Top
 Profile  
 
 Post subject: Re: Using Many to One Relationship
PostPosted: Sat Jul 24, 2010 5:12 am 
Newbie

Joined: Thu Mar 19, 2009 4:47 am
Posts: 8
Thanks for info. Your suggestion worked fine.

I have one more query:
I need to create database where multiple tables are joined with Many to one relationship.

E.g.
A related to B
B related to C
C related to D
D related to E
E related to F
and so on....

If I use many to one relationship will all child relations be loaded if a row for A is loaded? Please refer to your comment:
Be carefull, before saving a Student you will have to set his address with one yet present in DB

Thanks and Regards,
Vikash Anand.

Further to my first question if I need to edit records of A do I need to load all child rows and save it??


Top
 Profile  
 
 Post subject: Re: Using Many to One Relationship
PostPosted: Mon Jul 26, 2010 2:54 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
No, you don't.
The "cascade" property tells Hibernate what to do when persisting an entity (save or delete related data, ....).
When you load an entity, Hibernate behavior is determined by the "lazy" and "fetch" attributes. So when loading an A entity, Hibernate could fetch the entire DB or apply a lazy approach depending on the mappings. Refer to the documentation available on-line for an exhaustive explanation: http://docs.jboss.org/hibernate/core/3. ... mance.html

Stefano


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