Skip to main content
deleted 358 characters in body
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984

You don't need to use @Generated along with @GeneratedValue. The @Generated annotation is for the non-id entity attributes that are generated by the database during INSERT or UPDATE. For more details about the @Generated annotation, check out this article.

The sequence generator requires an extra database roundtrip to call the sequence object when you persist the entity. For this reason, Hibernate offers the sequence-based optimizers to reduce the number of roundtrips needed to fetch entity identifier values.

Hibernate pooled-lo optimizerHibernate pooled-lo optimizer

If you have been using the legacy hilo optimizer, you might want to switch to using pooled or pooled-lo, as hilo is not interoperable with other clients that might not be aware of the hilohilo identifier allocation strategy.

For more details about how you can migrate from hilo to pooled or pooled-lo, check out this article.

You don't need to use @Generated along with @GeneratedValue. The @Generated annotation is for non-id entity attributes that are generated by the database during INSERT or UPDATE. For more details about the @Generated annotation, check out this article.

The sequence generator requires an extra database roundtrip to call the sequence object when you persist the entity. For this reason, Hibernate offers the sequence-based optimizers to reduce the number of roundtrips needed to fetch entity identifier values.

Hibernate pooled-lo optimizer

If you have been using the legacy hilo optimizer, you might want to switch to using pooled or pooled-lo, as hilo is not interoperable with other clients that might not be aware of the hilo identifier allocation strategy.

For more details about how you can migrate from hilo to pooled or pooled-lo, check out this article.

You don't need to use @Generated along with @GeneratedValue. The @Generated annotation is for the non-id entity attributes that are generated by the database during INSERT or UPDATE. For more details about the @Generated annotation.

The sequence generator requires an extra database roundtrip to call the sequence object when you persist the entity. For this reason, Hibernate offers sequence-based optimizers to reduce the number of roundtrips needed to fetch entity identifier values.

Hibernate pooled-lo optimizer

If you have been using the legacy hilo optimizer, you might want to switch to using pooled or pooled-lo, as hilo is not interoperable with other clients that might not be aware of the hilo identifier allocation strategy.

Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984

@Generated vs @GeneratedValue

You don't need to use @Generated along with @GeneratedValue. The @Generated annotation is for non-id entity attributes that are generated by the database during INSERT or UPDATE. For more details about the @Generated annotation, check out this article.

On the other hand, the @GeneratedValue is only for the entity identifier attributes, and it's what you need to use when the entity identifier is generated automatically upon persisting an entity.

Sequence generator

The sequence generator requires an extra database roundtrip to call the sequence object when you persist the entity. For this reason, Hibernate offers the sequence-based optimizers to reduce the number of roundtrips needed to fetch entity identifier values.

Now, if you want to use hilo, the identifier mapping will look as follows:

@Id @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "post_sequence" ) @GenericGenerator( name = "post_sequence", strategy = "sequence", parameters = { @Parameter(name = "sequence_name", value = "post_sequence"), @Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "3"), @Parameter(name = "optimizer", value = "hilo") } ) private Long id; 

Apart from having to use the Hibernate-specific @GenericGenerator, the problem with hilo is that the generated identifiers don't include the database sequence value, so an 3rd-party client using the database will not know how to generate the next identifier value unless they know the hilo algorithm and the allocationSize.

For this reason, it's better to use pooled or pooled-lo.

Pooled optimizer

The pooled optimizer is very easy to set up. All you need to do is set the allocationSize of the JPA @SequenceGenerator annotation, and Hibernate is going to switch to using the pooled optimizer:

@Id @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "post_sequence" ) @SequenceGenerator( name = "post_sequence", sequenceName = "post_sequence", allocationSize = 3 ) private Long id; 

Hibernate pooled optimizere

Pooled-lo optimizer

To use the pooled-lo optimizer, just add the following configuration property:

<property name="hibernate.id.optimizer.pooled.preferred" value="pooled-lo" /> 

Now, the entity identifier mapping is identical to the one I showed you before for the pooled optimizer.

To understand how the pooled-lo works, check out this diagram:

Hibernate pooled-lo optimizer

If you have been using the legacy hilo optimizer, you might want to switch to using pooled or pooled-lo, as hilo is not interoperable with other clients that might not be aware of the hilo identifier allocation strategy.

For more details about how you can migrate from hilo to pooled or pooled-lo, check out this article.