File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate/MSSQL/identity column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate/MSSQL/identity column" Watch "Hibernate/MSSQL/identity column" New topic

Hibernate/MSSQL/identity column

Chris Wise

Joined: Jan 27, 2006
Posts: 3
Just recently started using a product that uses Hibernate. I am going against an MQ SQL Server database and have had success in fetching records and doing updates.

My problem is with insert. I am getting an error that Hibernate is attempting to specify the primary key identity field in the generated insert statement when it ought not be in the column list. I think I've got some sort of mapping configuration problem.

Please let me know if this mapping looks reasonable for an identity field.

<class name="com.murmurinformatics.Trackstar.persistence.PersistTSDatabases" table="ts_databases" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" optimistic-lock="version">

<id name="trackstar_db_id" column="trackstar_db_id" type="big_decimal" unsaved-value="null"><generator class="identity"/>

<property name="display_name" type="string" column="display_name" not-null="false" unique="false" insert="true" update="true"><column name="display_name"/></property>

Chris Wise

Joined: Jan 27, 2006
Posts: 3
As an addendum, I notice in the SQL generated by Hibernate it is specifying the identity column in the INSERT statement and providing a value of null.

I would expect when this is properly configured it wouldn't specify teh column or null value.
Chris Wise

Joined: Jan 27, 2006
Posts: 3
I figured it out - two problems:

1 - I had to change the identity property to a supported type of Long.

2 - My file kept getting reset by the tool I was using from SQLServer to HSQL. This was what caused it to generate NULL in the insert statement.
Samuel Thadikonda

Joined: Mar 27, 2007
Posts: 1
Hi Chris,

since it worked for you, cna you please give an insight into my problem !!

I am getting an exception while inserting rows in a table of MSSQLServer with Hibernate. My configuration is as follows. It works OK, if I change generator class to increment. I know both native as well as identity should work for SQLServer dialect. Any Clues?

<class name="DemoDTO" table="DEMO">
<id name="id" column="ID" type="long">
<generator class="identity" />
<property name="name" length="20"/>
<property name="description" />
<property name="status" />

The Exception is :
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert the value NULL into column 'id', table 'dbo.DEMO'; column does not allow nulls. INSERT fails.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Samual, it looks to me that the database table for that field is not defined as an identity column, but just a number.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Scott A Lynch

Joined: Jun 28, 2007
Posts: 2
I'm digging this thread up from the grave because I've got a similar issue to Chris's, but my symptoms are different. The error I get is this:

I've found a workaround (currently commented out) but I'm not really happy with the idea of having to put the SQL query for inserts in my class configuration. I'm connecting to SQL Server 2005.

Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Do you have a getter for endDate?

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Jason Perrone

Joined: May 14, 2010
Posts: 1
Mark Spritzler wrote:Samual, it looks to me that the database table for that field is not defined as an identity column, but just a number.


That was my problem. Thanks Mark!
I agree. Here's the link:
subject: Hibernate/MSSQL/identity column
It's not a secret anymore!