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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate + ms sql server + identity autoincrement
PostPosted: Thu Jan 13, 2011 7:19 pm 
Newbie

Joined: Thu Jan 13, 2011 7:00 pm
Posts: 2
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 =)


Last edited by Hazneliel on Fri Jan 14, 2011 12:34 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate + ms sql server + identity autoincrement
PostPosted: Thu Jan 13, 2011 9:25 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
it's nice you try to be helpfull, but sorry you'll have a lot of issues with this approach.

Also, there's no need for it, as Hibernate supports autoincremented identity very well, you're misinterpreting the error message:
Quote:
Dialect does not support identity key generation

doesn't mean as you say
Quote:
Hibernate does not support identity key generato

but it means that you are using the wrong Dialect. check your configuration, you're forcing Hibernate to use the wrong Dialect. Actually not configuring any dialect should fix your problem, as appropriate dialects are detected by default from the driver.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate + ms sql server + identity autoincrement
PostPosted: Fri Jan 14, 2011 12:34 pm 
Newbie

Joined: Thu Jan 13, 2011 7:00 pm
Posts: 2
Oh well, Im using hibernate 2 so maybe it is solved on hibernate 3 but the problem arises on hibernate 2.0
Sometimes is not posible to upgrade rigth away and we have to work around some issues.


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