JPA / Hibernate / MS SQL Server does not return the generated identifier value

I am using Hibernate 3.5.1-Final entity manager with MS SQL Server 2005 and trying to save several new objects. My object is annotated configured this way:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

After the call

entityManager.persist(newEntity)

I do not see the generated set, it remains equal to 0. This causes the following exception when saving the next new object:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.y.z.MyEntity#0]

I can get around this by pushing the recently saved entity out of the cache before I save the next object, but this is not ideal. What do I need to do to correctly update the object after insertion?

+3
source share
3 answers

, 0.

( IDENTITY long, short, integer - - String). :

  • db (, )?
  • SQL ( SQL)?
  • , SQL SQL-?

, , , .

. , . , . , .

0

, , :

em.persist({ENTITY_TO_PERSIST});
em.flush();
em.refresh({ENTITY_TO_PERSIST});

refresh() .

. , Identity, "yes" .

+2

"I".

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
0

Source: https://habr.com/ru/post/1760114/


All Articles