Hello, I had this problem for like 2 months until today I finally found a nice solution.
Problem:On Hibernate 2.0
You have a MS SQL Server table with a primary key assigned as identity and autoincrement, then you try to map that table to an hibernate object.
Hibernate does not support identity key generator, this error:
Code:
Dialect does not support identity key generation
It shows when you have a MS SQL Server Table with a primary key as identity and you try this in your mapping:
Code:
<generator class="identity"/>
You cannot use sequence because MS SQL SERVER does not suppor sequences =/
You cant neither use:
Code:
<generator class="increment"/>
Because what it really does is an inner select max(id) from a table and it increments it by 1 inserting it in the new row, but an identity column is read only, you would get some error about trying to assign thet value. Also for this reason you cant use any trick like having a sequence table just to make selects from it, because you wouldnt be able to insert them in an identity colum.
I also tried to omit the id tag in the hbm file, but I get an error about missing tags on my hbm xml file.
So, if you cant modify your primary key in the table because its already working with other programs, what can we do?
Solution:I was thinking how to not map the identity column in my hbm file, but still have an id tag at the same time. And if you came to this same question the answer comes by itself. You can cheat Hibernate to belive that any column is the primary id key, knowing this you just declare in MS SQL Server a dummy Id column as an integer and ofc, not primary key not identity and you map it in your hbm file as the id column.
Code:
<id
name="dummyId"
column="DummyId"
type="integer"
>
<generator class="increment"/>
</id>
Then Hibernate will save your object to the DB and MS SQL Server will autoincrement the real Id.
ofc there are some Drawbacks to this solution, rigth now I cant think about 1 or 2 like searching a row by Id, but you can easily search by the property which is the real Id in the Table. I would like to read any feedback.
I hope this helps a lot of programers who find this problem in the future =)