-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: identity problem with composite id
PostPosted: Thu Feb 04, 2010 12:02 pm 
Newbie

Joined: Thu Feb 04, 2010 11:20 am
Posts: 2
Hi,
I have a table that has columns A,B and C. “A” and “B” composes primary key together. And hbm.xml looks like ;
<class name="Table1" table="TABLE1">
<composite-id >
<key-property name="a" type="int">
<column name="A" />
</key-property>
<key-property name="b" type="int">
<column name="B" />
</key-property>
</composite-id>
<property name="c" column="C" not-null="true" />
</class>

And my problem is column “A” should be auto generated and column “B” should be assigned. For example table looks like this with data;

A B C
1 1 Ex1
2 1 Ex2
3 2 Ex3

And when inserting new value into table how can i auto increment column “A” and assign a custom value to column “B” using hibernate? Can i do that with defining column “A” as identity in database and how should this be specified in hibernate?


Top
 Profile  
 
 Post subject: Re: identity problem with composite id
PostPosted: Thu Feb 04, 2010 5:29 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
This is a pretty tricky question. I will watch this thread if some one responds back.
I don't think it is possible to do what you are looking for lets see if someone else has a solution for this.
The reason I don't think its possible is because database generated value will only be created when Hibernate issues an insert until the value is inserted, the generated value is not available to hibernate. So you might have to do is get the generated value for column "A" read the value by loading the object, then issue an update on the column "B" if the Column "A" and Column "B" values are unique per composite-id requirement, then the update will be successful other wise it will fail.

Question - what is the business requirement for this.


Top
 Profile  
 
 Post subject: Re: identity problem with composite id
PostPosted: Fri Feb 05, 2010 3:35 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From the Hibernate documentation at http://docs.jboss.org/hibernate/stable/ ... ompositeid

Quote:
You cannot use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.


But if column A is auto-generated it usually means that it is unique by itself. Why not use only A as the id?


Top
 Profile  
 
 Post subject: Re: identity problem with composite id
PostPosted: Fri Feb 05, 2010 4:09 am 
Newbie

Joined: Thu Feb 04, 2010 11:20 am
Posts: 2
Actually this table is about exchange and contains exchange offers. Offers can be divided into multiple parts. And then each part becomes an offer which are representing by orginal offer id. In this example column “A” describes offer id and “B” the partition of offer. For example after first offer insert the value for offer and partition id should be like A = 1, B=0. After division there will be 2 data for offer whose id = 1 and table should look like this;
A B
1 0
1 1
And with each division i will assign a value for “B” and give the original id to “A”. But when a new offer comes i want to increase the offer id “A”.
A B
1 0
1 1
2 0

However i realized you are right if i specify column “A” auto generated i wont able to assing value when a division occurs. So i decided define “A” and “B” assigned and i generate id for “A” from a sequence table which i’ll create.
Thanks a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.