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
|