aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Association table - extra column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Association table - extra column" Watch "Hibernate Association table - extra column" New topic
Author

Hibernate Association table - extra column

Gilbert johnson
Ranch Hand

Joined: Jul 10, 2005
Posts: 45
Hello,
I'm new to hibernate and am stuck on this issue for sometime now and would really appreciate some help.

I have two tables - GROUP and PRODUCT
Its a many to many association between them.
A group can have many products
A product can belong to many groups

So I have a join table - GROUP_PRODUCT which has groupID and productID.
But it also has an extra column userID

This is what I have added in my Group.hbm.xml file for this purpose


This is what I have product.hbm.xml for this purpose


Now when I try to create a group and add a product to this group, I see the following sql statements followed by this error:


If you look at the 2nd sql,the insert statement does not even include the extra field userID

I know I'm missing something very basic here. So if you could point me in the right direction - I would greatly appreciate it.

Thanks
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Gilbert,

I think you need to use an intermediate entity class (e.g., a class GroupProduct) and map one-to-many associations between it and the Group and Product classes. Alternatively, map the join table to a collection of components.

In your case, since it appears there's no ID column for the GROUP_PRODUCT table, you'd probably want to use the component collection method. Create your GroupProduct class, which will be very simple. Then map the Group and Product to use it. Example below for Product (may have typos, didn't write out test code)



Now to add to your set (assuming the correct constructor):



These are the two approaches outlined by Bauer and King in Java Persistence for Hibernate. I'm not sure there's a way you can add the extra column otherwise. If anybody else on the Ranch knows, I'd love to see the answer, but I've always used the intermediate entity class myself.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Gilbert johnson
Ranch Hand

Joined: Jul 10, 2005
Posts: 45
Hello Stevi,

Thankyou for your detailed response. I did try the way you suggested. Although I dont see any error - but nothing is being inserted into GroupProduct table.
I added what you provided in Product.hbm.xml

Should I add this in Group.hbm.xml


Thanks
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Gilbert,

Without seeing your full mapping files, etc., it's hard to isolate the cause of the problem.

I'd recommend as a next step downloading the Caveat Emptor example project on Hibernate.org. The native project is the one you'll want. Look at the mappings and the comments in the auction.model package, specifically Category.hbm.xml - there some good comments about the various approaches you can take.

Good luck!
Ahasan Habib
Greenhorn

Joined: May 26, 2010
Posts: 5

Hello Stevi,

I am facing problem in composite-element mapping of hibernate. My expected relation is many to many in document and task. That is a task may have multiple documents and a document may have assigned to multiple task. Relational table will contain both foregin key with additional column. In attachment i have given the tables structures.

My Task hbm file:


My document hbm file:


I have created all mapping files as you said. My question what will be the java code for inserting in TM_TASK table as well as for TM_TASKDOCS table. And what will be the TaskDocsInfo class exactly. where I have to create the composite class instance and have to assing this instance? I have tried with the following code but fails



Please help.
Thanks in advance



[Thumbnail for M2M.JPG]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Association table - extra column