views:

2221

answers:

1

I am getting JDBC error when I attempt a commit through hibernate to SQL Server

Cannot insert explicit value for identity column in table 'Report' when IDENTITY_INSERT is set to OFF

I am using mappings generated by netbeans that contain,

<class name="orm.generated.Report" table="Report" schema="dbo" catalog="DatabaseName">
    <id name="id" type="int">
        <column name="ID" />
        <generator class="assigned" />
    </id>

Which looks to me like it should be doing the identity insert properly.

Any idea on how to fix this?

EDIT:
Some links to documentation, for posterity,
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html#mapping-declaration-id-generator
http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml

+3  A: 

You cannot insert into an identity column in SQL Server unless "IDENTITY_INSERT" is set to "ON". Since your generator class is "assigned", Hibernate is assuming that you are setting an explicit value for "id" in Java before saving the object and that Hibernate can directly insert the value into the database. You need to either:

  1. Pick a different generator class, such as "native"
  2. Set IDENTITY_INSERT to "ON"
cliff.meyers
Thanks I will give this a try when I get back to work on Tuesday.
James McMahon
This works, leads me into another error, but it works, Thank you.
James McMahon
Just to clarify, I meant, by fixing this problem, I uncovered another one.
James McMahon