# Cannot persist a new record due to IDENTITY\_INSERT is set to OFF

**URL:** <https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95>\
**Category:** Hibernate ORM\
**Created:** [January 18, 2018, 9:33pm UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95 "2018-01-18T21:33:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.hibernate.org/u/developer)\
**Post date:** [January 18, 2018, 9:33pm UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95/1 "2018-01-18T21:33:10Z")

</div>

I have a bit of an interesting problem. I’m working on a what is a somewhat legacy system and encountered the following error trying to persist a new record into a specific table:

> Cannot insert explicit value for identity column in table ‘Table\_Name’ when IDENTITY\_INSERT is set to OFF.

I believe I found the root of the problem, what I believe is a limitation of hibernate, but a confirmation would be nice to ensure I’m not doing anything incorrectly.

The SQL Server table I’m working with looks something like this:

ID - Int, primary key  
Qty - Decimal  
Price - Decimal  
Order\_Sequence - Int, identity (+1)

Because Order\_Sequence is an identity and not the primary key, hibernate does not accept the insert. I found this brief document that may confirm this:

[http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html\_single/#d0e1150](http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1150)

I tried marking Order\_Sequence with @GeneratedValue and @GeneratedValue(strategy=“IDENTITY”) for but the error still occurs. Also setting IDENTITY\_INSERT to ON before the inserting does not work.

Can someone confirm this is a limitation?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 19, 2018, 5:51am UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95/2 "2018-01-19T05:51:11Z")

</div>

If the `Order_Sequence` column is the IDENTITY, then the Primary Key column must be manually assigned.

If that’s the case, then the ID column must be mapped like this:

```auto
@Id
@Column(name = "ID")
private Long id;

```

while the `Order_Sequence` column will be mapped as follows:

```auto
@Generated( value = GenerationTime.INSERT)
@Column(name = "Order_Sequence")
private Long orderSequence;

```

Try it like this and let me know how it works.

---

<div class="post-metadata">

**Author:** ![developer](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.hibernate.org/u/developer)\
**Post date:** [January 19, 2018, 3:53pm UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95/3 "2018-01-19T15:53:58Z")

</div>

That worked!

As I look at the documentation closer, @GeneratedValue is meant for primary key constraints only, which is why it was not working.

Thanks @vlad for your help.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 19, 2018, 4:09pm UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95/4 "2018-01-19T16:09:21Z")

</div>

You are very welcome.

---

<div class="post-metadata">

**Author:** ![yashkanrish](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yashkanrish/32/2336_2.png) [@yashkanrish](https://discourse.hibernate.org/u/yashkanrish)\
**Post date:** [March 23, 2023, 11:38am UTC](https://discourse.hibernate.org/t/cannot-persist-a-new-record-due-to-identity-insert-is-set-to-off/95/5 "2023-03-23T11:38:10Z")

</div>

Worked for me. Thanks @vlad
