• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Hibernate Mapping- composite id (pk+ foreign key)

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
In one of my tables, the composite key is consist of one auto increment key and one foreign key; this composite key is used as foreign key in some tables.
In the hibernate mapping, <composite-id> doesn’t allow <generator>, so i enabled the auto increment property for ID filed at data base level.

Because of this scenario, the records are getting inserted in parent as well as child table but the ID which is auto increment key is not getting inserted in child table where as it has been generated by database for respective record.

E.g.
In tb1 table, the composite key is consist of ,

ID - an auto increment key (at database level)

fk_ID - a foreign key

This composite key is used as a foreign key in tb2 and other table as well.

So when trying to insert the record, the ID has been generated and records are getting inserted in both the tables tb1 and tb2. but the value for ID in tb2 is null.

Can anybody tell me the solution?
one of the solutions for this problem, is using CompositeUserType for composite id class and then defining a custom IdentifierGenerator that populates the generated value into the composite key class.
But is there any other way?

Thanks,
Nishita

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another roundabout way would be to get the generated id from the parent table and set it manually in the child table.
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
THnaks for your reply.
you are saying to get the generated id from the parent table . but the problem is that pk id in parent table is generated by db not by hibernate. SO i am not able to load the parent object. For loading it i need id but id is not there in hibernate session.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant was that you would save the parent first and then use the generated id in the child to save it later.
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am not getting your point.
db wil create pk den save the obj .If i try to load the same obj using other properties der me multiple objects .
so point is how to get that generated Id.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm facing exactly the same problem. Have you solved it yet?
I'm beginning to think that a composite-id that consists of "normal" fields and a field that is auto-incremented by the DB doesn't make any practical sense, as an auto-incr. field is by itself unique.
Anyway, I'm still very curious as to how this could be done with Hibernate. Hibernate doesn't allow an AI generated PK as part of a composite-id, therefore it has no knowlege about what the DB is doing with its PK fields. The only workaround I can think of is to auto-increment the composite-primary-key field programatically i.e. by hand?
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic