-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Looking up related records in two tables?
PostPosted: Thu Sep 22, 2005 12:35 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I have two tables, one called Guest and and another called Reservation, in a MySQL database. My MySQL database install does not seem allow me to use ForeignKeys (apparently they are only support by INNODB tables).

The Guest table has columns ( guestId, lastName, firstName, phone ).
The Reservation table has columns ( id, date, time, guestId ).

One guest can have multiple reservations and each reservation refers to only one guest.

I want to be able to do a lookup for any reservations in the name of the above specifies guest and then return Reservation instances with the getGuest() refering to a Guest instance.

So far I have the following query:
Code:
      Criteria criteria = session.createCriteria(Reservation.class)
      .createAlias ("guest", "guest0")
      .add(Expression.eq("lastName",lastName));

but this does not seem to work. I suspect my problem must be in my definition file. The line I have in my Reservation.hbm.xml files is:
Code:
      <one-to-one
         name="guest"
         foreign-key="guestId"
         class="com.symphonize.eresbook.domain.Guest"/>

Can anyone suggest what I should be doing?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 12:50 pm 
Beginner
Beginner

Joined: Fri Aug 13, 2004 3:07 pm
Posts: 44
Here is what I would suggest.

Define a many to one relationship between the Reservation and Guest since that is what you want to do (many reservations for a guest)

use the query as

Code:
select r from Reservation r where r.guest.lastname = :lastname

in your query replace the :lastname named parameter with the actual lastname of the guest.

Hope it helps

_________________
Thanks
Sameet


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 5:28 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I followed the instructions above, making my many-to-one as follows:
Code:
      <many-to-one
           name="guest"
           column="guestId"
           class="com.symphonize.eresbook.domain.Guest"
           fetch="join"/>


and following the instructions and everything works :)

A quick note: I have been closing my sessions after every query, though this approach seems to break here, if you are using lazy loading:
Code:
org.hibernate.LazyInitializationException:
  could not initialize proxy - the owning Session was closed


As an added question, is this doable using Criteria, rather than HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 5:54 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
If you want to access something that is lazy loaded after the session is closed then it is best to initialize it before you close the session. There are other ways but initializing what you're going to need before the session is closed will cause you the least amount of headaches.

Either access the property or use Hibernate.initialize()

And to get your original working with Criteria the following should do the trick

Code:
      Criteria criteria = session.createCriteria(Reservation.class)
      .createAlias ("guest", "guest0")
      .add(Expression.eq("guest0.lastName",lastName));


Once you create the alias you have to prefix your property with it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 6:18 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
ajmas wrote:
I followed the instructions above, making my many-to-one as follows:
Code:
      <many-to-one
           name="guest"
           column="guestId"
           class="com.symphonize.eresbook.domain.Guest"
           fetch="join"/>


and following the instructions and everything works :)

A quick note: I have been closing my sessions after every query, though this approach seems to break here, if you are using lazy loading:
Code:
org.hibernate.LazyInitializationException:
  could not initialize proxy - the owning Session was closed


As an added question, is this doable using Criteria, rather than HQL?


How did you get around *not* closing the session? I'm having the same problem but the *only* action I'm doing is retriving a parent/child object/collection relationship...but I'm getting this error even though I don't close the session until *after* I query the data!

When *do* I close a session then?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 6:36 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
zambizzi wrote:

How did you get around *not* closing the session? I'm having the same problem but the *only* action I'm doing is retriving a parent/child object/collection relationship...but I'm getting this error even though I don't close the session until *after* I query the data!

At this point in time I am not closing it. I just commented out the 'session.close()' methods at this time. I was going to work on the main issues I had before dealing with this one. What are the errors you are getting?


Top
 Profile  
 
 Post subject: hmmm
PostPosted: Thu Sep 22, 2005 6:39 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
The same one you are...LazyInitializationError - session is closed, yada yada.

Not closing the session is out of the question though....that causes connection pool leaks, doesn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 6:56 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
The following link is one persons view on what to do, with regards to closing sessions:

http://blog.exadel.com/?p=8

The real answer seems to be: "it depends on what you are doing".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 9:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The advice in that blog entry is far inferior to the session management done by Seam. If you want that kind of functionality, use Seam-managed sessions which have conversation scope.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 10:40 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
gavin wrote:
The advice in that blog entry is far inferior to the session management done by Seam. If you want that kind of functionality, use Seam-managed sessions which have conversation scope.


Which would require JBoss as the application server, correct? What if I'm already committed, beyond my control, and cannot use JBoss?

Hibernate session scope management is strangely complex.

Is there another answer beyond using JBoss?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 10:58 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
ajmas wrote:
The following link is one persons view on what to do, with regards to closing sessions:

http://blog.exadel.com/?p=8

The real answer seems to be: "it depends on what you are doing".


Yeah, and this is what I'm reading all over the place...however I don't see *any* decent, real world examples...or some scenarios (with code).....something simple!!

I apologize but I'm frustrated after 1 week of Hibernate...I don't see where I've saved *any* time, effort, or lines of code....it's just shifted the complexity from one place to another.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 11:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, Seam is not in any way dependent upon JBoss AS, and the Seam Hibernate support does not require any application server at all.

http://docs.jboss.com/seam/reference/en ... ation.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 12:21 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
gavin wrote:
No, Seam is not in any way dependent upon JBoss AS, and the Seam Hibernate support does not require any application server at all.

http://docs.jboss.com/seam/reference/en ... ation.html


I skimmed through the doc, seems easy enough, however, will this be something that will be fully integrated into Hibernate in a future release? The whole session management thing could really use some reduction in complexity...in my own experience that's the worst part of using Hibernate!

I'll give it a shot, thanks Gavin.

-v


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 1:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There is no way to reduce the "complexity" of session management without being in control of the request lifecycle. Since Hibernate is a *library*, that runs with any framework you like, and is never aware of the request lifecycle, we have not really been able to address this issue. We left it to the frameworks to solve this problem.

At some stage we realized that the existing frameworks are doing an utterly awful job of session management, and not improving, and that Hibernate is getting the blame. (Just like you in this thread.) So this was one of the reasons we built our own framework. To do it properly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 1:56 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
gavin wrote:
There is no way to reduce the "complexity" of session management without being in control of the request lifecycle. Since Hibernate is a *library*, that runs with any framework you like, and is never aware of the request lifecycle, we have not really been able to address this issue. We left it to the frameworks to solve this problem.

At some stage we realized that the existing frameworks are doing an utterly awful job of session management, and not improving, and that Hibernate is getting the blame. (Just like you in this thread.) So this was one of the reasons we built our own framework. To do it properly.


I'm not really trying to place any blame...I guess I was just looking to shake the tree until a simple answer, like the one you've provided, falls out.

I've resorted to an ugly hack which appears to "work" but really isn't good design. I have a servlet which talks to a simple facade which talks to my object model, data access, and so on. Hibernate is located in the DAO portion of my app. I simply wrapped the session creation/destruction in two methods called "open" and "close"....I wrapped them in the facade which are then called by the servlet....which ensures a single session per request...as I've seen recommened in a few places while googling around.

This stopped the LazyInitializationException issue...but is so ugly it can't be good (ha!)

Anyhow, thanks for the straight answer...I'll see what I can do w/ Seam.

-v


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.