# Generated ID always zero

**URL:** <https://discourse.hibernate.org/t/generated-id-always-zero/6676>\
**Category:** Hibernate ORM\
**Created:** [September 20, 2022, 9:52am UTC](https://discourse.hibernate.org/t/generated-id-always-zero/6676 "2022-09-20T09:52:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![francesco\_disciascio](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/francesco_disciascio/32/2022_2.png) [@francesco\_disciascio](https://discourse.hibernate.org/u/francesco_disciascio)\
**Post date:** [September 20, 2022, 9:52am UTC](https://discourse.hibernate.org/t/generated-id-always-zero/6676/1 "2022-09-20T09:52:25Z")

</div>

The stack is the following: Spring MVC with Hibernate 4.2.1  
Saving entities in Oracle database works correctly: after save() I get the new identifier given by the DB sequence; when I switch to MS Sql Server instance, the save() returns always 0.  
Here is the ID field definition:

```auto
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "seq")
    @SequenceGenerator(name="seq", sequenceName="SEQ_PEZZI") 
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;

```

In debug mode, I’ve noticed that the problem is in the following line of code (in AbstractSaveEventListener.java, line 117 saveWithGeneratedId() method):

`Serializable generatedId = persister.getIdentifierGenerator().generate( source, entity );`

the `getIdentifierGenerator()` returns POST\_INSERT\_INDICATOR (while with Oracle, it returns the class name of the entity to save)

Is there some configuration missing, or is it a bug of the Hibernate version?  
Thanks for your help

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [September 20, 2022, 2:30pm UTC](https://discourse.hibernate.org/t/generated-id-always-zero/6676/2 "2022-09-20T14:30:29Z")

</div>

Hard to tell what is going on. You are using a very old version of Hibernate. Just know that sequences are only supported as of SQL Server 2012. If the same issue happens on Hibernate 5.6, we can take a look but for that, I would need a test case([hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)) that reproduces the issue.

---

<div class="post-metadata">

**Author:** ![francesco\_disciascio](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/francesco_disciascio/32/2022_2.png) [@francesco\_disciascio](https://discourse.hibernate.org/u/francesco_disciascio)\
**Post date:** [September 20, 2022, 2:44pm UTC](https://discourse.hibernate.org/t/generated-id-always-zero/6676/3 "2022-09-20T14:44:41Z")

</div>

Just to add further informations, in SQL Server there is a trigger INSTEAD OF INSERT that sets the ID from a sequence with this snippet:

```auto
IF (@NEW$ID < 0 OR @NEW$ID IS NULL)
BEGIN
     SELECT @NV = NEXT VALUE FOR dbo.SEQ_PEZZI
     SET @NEW$ID = @NV
 END

```

the SQL Server instance version that I am using is the 15.  
I will try to move to 5.6 Hibernate version when possible, but it is a real strange behaviour that I suppose it could depend more from SQL Server table configuration than from Hibernate…
