Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Hibernate/MSSQL/identity column

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a getter for endDate?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic