views:

33

answers:

1

I am using Hibernate over SQL Server for a web application. I was using the "increment" generator class till now and recently ran into ConstraintViolation exceptions.

So, I changed the generator to "identity" and I am getting the following error -

Cannot insert the value NULL into column 'assessment_administration_id', table 'faip.dbo.assessment_administration'; column does not allow nulls. INSERT fails.

Following is the mapping -

<class name="AssessmentAdministration" table="assessment_administration">
  <id name="id" type="long" column="assessment_administration_id">
    <generator class="identity" />
  </id>
  <property name="administrationName" column="administration_name" />
</class>

What am I doing wrong here? I looked at this post http://www.coderanch.com/t/216051/ORM/java/Hibernate-MSSQL-identity-column, but the solution is not posted.

A: 

Is your ID column actually an IDENTITY column? If it isn't, then that's your problem since Hibernate won't include the ID column in an insert statement when using an identity generator, relying on the database to generate the value. Did you alter the table after changing the generator strategy?

Pascal Thivent
What is an IDENTITY column? Do I have to alter the database table for that? Please note that I am using SQL Server 2008
Zoheb Borbora
@Zoheb An [IDENTITY](http://msdn.microsoft.com/en-us/library/aa933196%28SQL.80%29.aspx) column is a column type allowing to get automatically incrementing identification numbers. Since you were using another generator, I'm assuming that your ID column is currently not an IDENTITY column and that you need to change it.
Pascal Thivent
Thanks Pascal! Looks like this should work. I will try this and update the thread.
Zoheb Borbora
It seems like there is no straightforward way to convert an existing column into an IDENTITY column, refer http://www.mssqltips.com/tip.asp?tip=1397. I am thinking of writing a custom generator function to use in Hibernate. Suggestions?
Zoheb Borbora
@Zoheb I'm not a MS SQL Server expert, I can't answer that question. All I can say is that the goal with your new Hibernate mapping is to use an `IDENTIY` column with a start value greater than the current max value. I just don't know what's the best way to do that. But this is somehow a different question and, instead of writing your own generator, my suggestion would be to ask how to move to an identity column with SQL Server in another specific question.
Pascal Thivent
Thanks Pascal! ..
Zoheb Borbora