Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate/MSSQL/identity column

 
Chris Wise
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"/>
</id>

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

</class>
 
Chris Wise
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out - two problems:

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

2 - My hibernate.properties 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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" />
</id>
<property name="name" length="20"/>
<property name="description" />
<property name="status" />
</class>

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
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samual, it looks to me that the database table for that field is not defined as an identity column, but just a number.

Mark
 
Scott A Lynch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ideas?
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a getter for endDate?
 
Jason Perrone
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark


That was my problem. Thanks Mark!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic