-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite primary key and auto-increment, please help
PostPosted: Wed Oct 26, 2005 4:34 pm 
Newbie

Joined: Sun May 08, 2005 2:33 pm
Posts: 6
Hello All,

I have a table with a composite primary key consisting of two integer columns where one of them is auto-increment (SQL Server IDENTITY). I cannot change the database schema as this is a legacy database I have to support. The problem is, I cannot use <generator> inside <composite-id> anywhere. How do I solve this problem? Hibernate tries to explicitly write a value into auto-increment column when I save and there is no obvious way to tell it to stop doing that.

table def:

CREATE TABLE [SURVEY_ANSWERS] (
[ANSWER_ID] [int] IDENTITY (1,1) NOT NULL,
[STATUS] [int] NULL,
[QUESTION_ID] [int] NOT NULL,
[POSITION] [int] NULL,
[TEXT] [varchar](350) NULL,
[VALUE] [int] NULL

);
GO
ALTER TABLE SURVEY_ANSWERS
ADD CONSTRAINT pk_SURVEY_ANSWERS PRIMARY KEY(ANSWER_ID,QUESTION_ID);
GO


Thanks,

Denis.


Top
 Profile  
 
 Post subject: custom generator
PostPosted: Wed Oct 26, 2005 6:19 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
It looks like you need to implement custom ID generator
http://www.hibernate.org/hib_docs/v3/re ... aration-id

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: custom generator
PostPosted: Wed Oct 26, 2005 7:40 pm 
Newbie

Joined: Sun May 08, 2005 2:33 pm
Posts: 6
kgignatyev wrote:
It looks like you need to implement custom ID generator
http://www.hibernate.org/hib_docs/v3/re ... aration-id


Konstantin,

But <generator> can only be specified inside <id>, it is not allowed for <composite-id>, is it?

Denis.


Top
 Profile  
 
 Post subject: hmm
PostPosted: Thu Oct 27, 2005 11:07 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
You are correct, generator is for ID.

I looked at your problem again and now I do not quite understand why do you need to model your class with the composite key. It looks like DB identity column is the unique identifier for a row in the database. And question_id is just the FK to the question. Having them combined in PK does not make sense for me, but that does not matter much.

I suggest mapping as if ANSWER_ID is the sole ID of the answer and have Question mapped as normal many-to-one property.


If you still decide that you need to have composite key, then you may want to investigate org.hibernate.event package
http://www.hibernate.org/hib_docs/v3/re ... ate-events
and may try to define custom class for the identifier…

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: hmm
PostPosted: Thu Oct 27, 2005 11:24 am 
Newbie

Joined: Sun May 08, 2005 2:33 pm
Posts: 6
Thanks a lot, Konstantin. I will use the approach you suggested.


kgignatyev wrote:
You are correct, generator is for ID.

I looked at your problem again and now I do not quite understand why do you need to model your class with the composite key. It looks like DB identity column is the unique identifier for a row in the database. And question_id is just the FK to the question. Having them combined in PK does not make sense for me, but that does not matter much.

I suggest mapping as if ANSWER_ID is the sole ID of the answer and have Question mapped as normal many-to-one property.


If you still decide that you need to have composite key, then you may want to investigate org.hibernate.event package
http://www.hibernate.org/hib_docs/v3/re ... ate-events
and may try to define custom class for the identifier…


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.