File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

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

Gilbert johnson
Ranch Hand

Joined: Jul 10, 2005
Posts: 45
Hello,
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.
Thanks

Here are my hbm.xml files
Group.hbm.xml


GroupProduct.hbm.xml


Product.hbm.xml

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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.

Mark


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