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 specifying a Postgres sequence to use via annotations.

However, I have an id column defined with the 'serial' type, I have read that I can simply use GenerationType.IDENTITY and it will automatically generate a db sequence and use it to auto increment.

If that's the case, I don't see an advantage to using the SEQUENCE annotations unless you are using an integer for an id or have some specific reason to use another sequence you have created. IDENTITY is alot less code and potentially makes it portable across databases.

Is there something I'm missing?

Thanks in advance for the feedback.

If you have a column of type SERIAL, it will be sufficient to annotate your id field with:

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)

This is telling Hibernate that the database will be looking after the generation of the id column. How the database implements the auto-generation is vendor specific and can be considered "transparent" to Hibernate. Hibernate just needs to know that after the row is inserted, there will be an id value for that row that it can retrieve somehow.

If using GenerationType.SEQUENCE, you are telling Hibernate that the database is not automatically populating the id column. Instead, it is Hibernate's responsibility to get the next sequence value from the specified sequence and use that as the id value when inserting the row. So Hibernate is generating and inserting the id.

In the case of Postgres, it happens that defining a SERIAL column is implemented by creating a sequence and using it as a default column value. But it is the database that is populating the id field so using GenerationType.IDENTITY tells Hibernate that the database is handling id generation.

These references may help:

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

PostgreSQL SERIAL column and Hibernate IDENTITY generator , Learn how the PostgreSQL SERIAL column type works and why you However, this article will show you that this is not a very good idea when using JPA and Hibernate. The Post entity id property uses the GenerationType. GenerationType.IDENTITY. The GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view. It relies on an auto-incremented database column and lets the database generate a new value with each insert operation.

I think it can be helpful if you are using the same sequence for more than one table (for example you want a unique identifier for many types of bills) ... also If you want to keep track of the sequence away from the auto generated key

Hibernate with PostgreSQL, The JPA specification defines different strategies to generate primary key values It uses a simple database sequence and is highly optimized by PostgreSQL. @Id. @GeneratedValue (strategy = GenerationType.SEQUENCE). @Column  JPA Tutorial - JPA ID GenerationType IDENTITY Example « Previous; Next » When using the auto generated value for id field we can select generation strategy.

From "Pro JPA2" book:

"Another difference, hinted at earlier, between using IDENTITY and other id generation strategies is that the identifier will not be accessible until after the insert has occurred. Although no guarantee is made about the accessibility of the identifier before the transaction has completed, it is at least possible for other types of generation to eagerly allocate the identifier. But when using identity, it is the action of inserting that causes the identifier to be generated. It would be impossible for the identifier to be available before the entity is inserted into the database, and because insertion of entities is most often deferred until commit time, the identifier would not be available until after the transaction has been committed."

How to use Hibernate identifier sequence generators properly , BIGSERIAL column types are not a very good choice when using JPA and Hibernate. Using a SEQUENCE generator is a better alternative since the @​Entity public class User { @Id @GeneratedValue(strategy = GenerationType. If we are using Postgres, defining the id column as the SERIAL type will  When using PostgreSQL, it’s tempting to use a SERIAL or BIGSERIAL column type to auto-increment Primary Keys. However, this article will show you that this is not a very good idea when using JPA and Hibernate. SERIAL or BIGSERIAL. If you’ve been using MySQL, you know that AUTO_INCREMENT is a very popular choice.

postgresql bigserial or sequence using jpa, For JPA, you should use @GeneratedValue(strategy=GenerationType.IDENTITY) for PostgreSQL serial/bigserial columns. You could also use PostgreSQL SERIAL column by specifying @GeneratedValue(strategy=GenerationType.IDENTITY) annotation. But a SequenceGenerator performs better in this case. 3. Answer model. Following is the definition of Answer class. It contains a @ManyToOne annotation to declare that it has a many-to-one relationship with the Question entity.

JPA и PostgreSQL с GenerationType.IDENTITY, Если у вас есть столбец типа SERIAL , будет достаточно, чтобы аннотировать id поле с: @Id @GeneratedValue(strategy=GenerationType.​IDENTITY). Introduction As I already mentioned, you should never use the TABLE identifier generator since it does not scale properly. In this post, I’ll show you why you should not rely on the AUTO GenerationType strategy if you’re Hibernate application uses MySQL. Prior to Hibernate 5 On Hibernate 4, if you had the following entity mapping: When persisting 3 Post entities: Hibernate would generate

JPA et PostgreSQL avec GenerationType.IDENTITY, Si vous avez une colonne de type SERIAL , il sera suffisant pour annoter votre champ id avec: @Id @GeneratedValue(strategy=GenerationType.IDENTITY). JPA (which stands for Java Persistence API) is a Java specification for accessing, persisting, and managing data between Java objects/classes and relational databases (e.g. PostgreSQL, MySQL, SQLServer, etc). The process of mapping object-oriented entities to entity-relationship models is also know as ORM (Object-Relation Mapping) and JPA is

Comments
  • One is generated on INSERT, and the other is not. One is not known in PrePersist, the other is known.
  • Great. Thank you for confirming my understanding of this!
  • @Mike could you, please, mark it as a correct answer