-->
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.  [ 5 posts ] 
Author Message
 Post subject: Simple Question about Mapping And HQL
PostPosted: Fri Jun 27, 2008 4:55 am 
Newbie

Joined: Wed Jun 25, 2008 5:24 am
Posts: 7
Hi All,

Am new to hibernate, i have a simple question,

in the mapping file if we declare the one to many relations for tables, then to select values from both the tables, to select values from both the tables we need to write HQL with join or simply select all will select according to the one to many relation in the mapping file???


for example i have two tables table1 and table2,

in table one's mapping file, for the ID in table1 column i have given one to many relation to the table2, to select the values according to ID from both the tables, we need to write the HQl with Join or select all from table1 will fetch the values from table2 aslo??

Please clear my doubt........

Thanks,
vyrav.


Top
 Profile  
 
 Post subject: Ans to simple query
PostPosted: Fri Jun 27, 2008 5:08 am 
Newbie

Joined: Fri Jun 27, 2008 4:52 am
Posts: 1
Location: Delhi
Hi VYRav,
You just need to write "select from bean1(of table1)" and it will fetch all the data to the bean2(of table2) which is a member variable of bean1.
Hibernate internally manages the queries and reduces the code for you.This is the beauty of hibernate.
Regards
Shail[/i]

_________________
Knowledge is Strength


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 9:48 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
In fact, it might even be easier than doing an HQL query in this scenario.

Say a User has many Addresses. If I know the primary key of the User, I can just load the User directly.

Code:
User user = (User)session.load(User.class, 1); // something like that


Then, all of the associated addresses are available through that user isntance, so I could do somthing like this:

Code:
Address primaryAddress = user.getAddresses().get(0); //something like that


One thing I will say is that your Hibernate Session will need to be open as you loop through the addresses. Don't close your session too early, or you'll get a LazyInitializationException, and we hate those!

Here's a great little tutorial on mapping one to many relationships. If you're new to Hibernate, you'll find it really useful. Check out the link on How Hibernate Works as well.




http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=18mappingonetomanyassociations

_________________
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:
PostPosted: Mon Jun 30, 2008 1:12 am 
Newbie

Joined: Wed Jun 25, 2008 5:24 am
Posts: 7
Thank you guys, you cleared my doubt.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 10:41 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Always a pleasure!

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