-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate composite primay key with autogenerated value
PostPosted: Mon Aug 10, 2009 2:26 am 
Newbie

Joined: Mon Aug 10, 2009 2:15 am
Posts: 1
Hi folks,

I am using Hibernate 3.3 with a SQL Server 2005 database. In the database, we have a table having a composite primary key of 2 columns, in which one of the columns is auto-generated. I am unable to insert values to the table using Hibernate. I cannot change the DB design because it is used by other applications as well.

Which is the best way to define a composite key which contains auto-generated values? I know that the <generator class = "...." /> tag cannot be used with the <composite-id> or <key-property> tags at all, so how do we specify that this value is auto-generated? I have declared a separate class and set it as the identity, using:

<composite-id name = "identifier" class = "Identifier">
<key-property name = "col1" column = "COL_1" /> <!-- This column is auto generated -->
<key-property name = "col2" column = "COL_2" />
</composite-id>

However, while inserting a value into the table, it fails. It gives this error message:

"Cannot insert explicit value for identity column in table 'TAB_1' when IDENTITY_INSERT is set to OFF."

I have tried setting the value explicitly with identifter.setCol1 () too (though I shouldn't because its supposed to be auto-generated). In either case, the error is the same.

Thanks & Regards,
Gurvinder Pal Singh


Top
 Profile  
 
 Post subject: Re: Hibernate composite primay key with autogenerated value
PostPosted: Wed Sep 02, 2009 8:18 am 
Newbie

Joined: Wed Sep 02, 2009 7:55 am
Posts: 1
hi,

i had the same prob

figured is out

if u have an SQL server DB then in the cfg.xml at dialect property insert:
org.hibernate.dialect.SybaseDialect

in u're hbm.xml set the id <generator class="identity"/>
and type="long"

thats all i needed

cheers,

Laszlo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.