-->
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: Easy retrieval of child records
PostPosted: Thu Apr 12, 2007 5:23 pm 
Beginner
Beginner

Joined: Tue Mar 13, 2007 9:46 am
Posts: 20
I am a Hibernate beginner and have a really simple case, that I can not seem to find the solution to. I have searched tutorials and references, but I must have missed something.

I have a table "header_table" that is a parent table. I also have a "line_table" that are child records of the "header_table" (one-to-many) - one header-record can have multiple line-records. The column linking these two tables are header_id

What I want to do is to retrieve one header_table-record together with all line_table-records for that record.

Can someone help me with:

* What to put in the mapping files
* What to put in the java-files of header_table.java and line_table.java
* Some simple lines of testcode for retrieving this

I've tried so many different things, and nothing works. It only seems to load the header_table...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 12:06 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that lines have line numbers, the mapping in your <class name="Header" ...> is
Code:
<list name="lines">
  <key="header_id"/>
  <index column="line_number"/>
  <one-to-many class="Line"/>
</list>
And you'll need this in your Lines class' mapping:
Code:
<many-to-one name="header" class="Header" column="header_id"/>
You'll need the appropriate properties in the two classes, too. Headers needs a "List<Line> getLines()" and "void setLines(List<Line> lines)" (never call the set method, it's just for hibernate), and it should have a default value (private List<Line> m_lines = new ArrayList<Line>();). Lines needs "Header getHeader()" and "void setHeader(Header header)"..

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:22 pm 
Beginner
Beginner

Joined: Tue Mar 13, 2007 9:46 am
Posts: 20
Thanks for your answer. What Java code do I write to make Hibernate perform the query?

will .getLines() force a SQL query?

Stein Rune


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 14, 2007 10:10 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Yes, it will execute a SQL query if the collection has not been loaded and if the code is being executed within an active transaction. If there is not transaction, a LazyInitializationException will be thrown.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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.