JPA and PostgreSQL with GenerationType.IDENTITY

I have a question about Postgres and GenerationType.Identity vs Sequence

In this example ...

@Id
@SequenceGenerator(name="mytable_id_seq",
                   sequenceName="mytable_id_seq",
                   allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator="mytable_id_seq")

I understand that I am setting the Postgres sequence to use through annotations.

However, I have an identifier column defined using the type "serial", I read that I can just use GenerationType.IDENTITY, and it will automatically generate a db sequence and use it to automatically grow.

If in this case I do not see the advantage of using SEQUENCE annotations, if you do not use an integer for the identifier or have no specific reason to use another sequence you created. IDENTITY is much less code and potentially makes it portable across databases.

Is there something I am missing?

.

+4
2

SERIAL, id :

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)

Hibernate, id. , "" Hibernate. Hibernate , , , id , - .

GenerationType.SEQUENCE, Hibernate, id. , Hibernate id . , Hibernate id.

Postgres , SERIAL . , id, GenerationType.IDENTITY Hibernate, .

:

http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators

https://www.postgresql.org/docs/8.1/static/datatype.html#DATATYPE-SERIAL

+4

, , (, )...

0

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


All Articles