This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate - Using Join table - one-to-many association - Lazy Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - Using Join table - one-to-many association - Lazy Exception" Watch "Hibernate - Using Join table - one-to-many association - Lazy Exception" New topic

Hibernate - Using Join table - one-to-many association - Lazy Exception

Gilbert johnson
Ranch Hand

Joined: Jul 10, 2005
Posts: 45
First of all - want to thank everyone who contributes in this forum. It has helped me immensely so many times when I have been stuck.

Right now I am on my quest to learn hibernate and have already asked questions here before.

I have two tables - Group and Product which have a bi-directional many-to-many relationship. A group can have many products and a product can belong to many groups.

So I introduced a join table "Group_Product" - now this table has an extra column userID. So one of the members of this forum suggested two approaches - and I went with the approach of using two one-to-many associations. Everything is working fine.

Now I want to retrieve all products that belong to a particular Group.
In my Java Code - using groupId, I'm loading the Group object.
From my Group object - I'm getting a set of GroupProduct.
Now GroupProduct has objects Group and Product and userId as members.

Now in my java code, I have:

The code is printing userID and productID but throws an exception while printing productName.

I know thats because, its only querying Group and Group_Product table. Its not linking Group_Product table with Product table.

What am I missing. Really appreciate your responses.

Here are my hbm.xml files



[ March 26, 2008: Message edited by: Gilbert johnson ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

LazyInitializationExceptions occur when Hibernate tries to get data from the database, but there is no longer a Session open. Basically, where you are trying to access the other data Hibernate wants to run a query against the database, but it can't get a Connection to the database, which it gets from the Session object, which is no longer open or available.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Hibernate - Using Join table - one-to-many association - Lazy Exception
Similar Threads
Hibernate (HQL) and many-to-many issues
Many-to-many probelm help
Hibernate: one-to-one composite foreign key : Strange Scenario
Hibernate Association table - extra column
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.