• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Association table - extra column

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gilbert johnson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
M2M.JPG
[Thumbnail for M2M.JPG]
Table Structure
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic