How to efficiently determine the automatically generated identifier for a new object when using JPA?

I have an attribute that is annotated using @Id. An identifier will be generated automatically when the object is saved. This means that the ID value is not defined before I save the object. After saving it, it has an identifier (in the database), but, unfortunately, the field still remains empty until I reload it from the database. is there an easy way to find out the generated id? Or better: configure it to be written in the field?

Thanks in advance

+3
source share
1 answer

. , Foo id, @Id @GeneratedValue, :

Foo foo = new Foo();
//...
em.persist(foo);
em.flush();
assert foo.getId() != null;
+6

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


All Articles