Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiberate problems, jdbc IDENTITY_INSERT is set to OFF

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

like image 523
James McMahon Avatar asked Jan 16 '09 22:01

James McMahon


People also ask

What is when Identity_insert is set to off?

IDENTITY_INSERT off in SQL Server Once you have turned the IDENTITY_INSERT option OFF, you cannot insert explicit values in the identity column of the table. Also, the value will be set automatically by increment in the identity column if you try to insert a new record.

What is Identity_insert is set to ON?

The set identity_insert command in SQL Server, as the name implies, allows the user to insert explicit values into the identity column of a table.


1 Answers

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"
like image 172
cliff.meyers Avatar answered Sep 23 '22 16:09

cliff.meyers