Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to dynamically choose a @GeneratedValue strategy using JPA annotations and Hibernate?

Tags:

I am working on a product that will be supporting multiple database engines (Oracle, MSSQL, MySQL). For Oracle, I would prefer to use Sequences rather than a Sequence table to avoid potential concurrency and locking issues on a high-volume installation, but other database engines do not support sequences. Furthermore, I would prefer to use one sequence per table rather than a global sequence (such as hibernate_sequence), so @GeneratedValue(strategy = GenerationType.AUTO) won't work. Is there a way to dynamically choose the strategy at runtime?

like image 207
Clay Avatar asked Oct 04 '12 22:10

Clay


People also ask

Why we are using JPA annotation instead of hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.

What is the use of @GeneratedValue strategy GenerationType Auto?

GenerationType. AUTO This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.

What is the use of @GeneratedValue annotation?

Annotation Type GeneratedValueProvides for the specification of generation strategies for the values of primary keys. The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.

What is the default strategy of of GeneratedValue?

AUTO indicates that the persistence provider should select an appropriate strategy based on the specified database dialect. The AUTO will automatically set the generated values. It is the default GenerationType. If no strategy is defined, it will consider AUTO by default.


1 Answers

Actually, Hibernate interprets both GenerationType.AUTO and GenerationType.SEQUENCE using its org.hibernate.id.enhanced.SequenceStyleGenerator. SequenceStyleGenerator is an id generation strategy that picks one of two strategies based on what the underlying database supports. If the database supports sequences, SequenceStyleGenerator uses sequences; if it does not, SequenceStyleGenerator falls back to using a "sequence table". This "mapping" of which generator to use is controlled by a setting: hibernate.id.new_generator_mappings. Setting that to true enables the behavior I just described above. Unfortunately, for backwards compatibility reasons, we had to default that to false. So to take advantage of that, you'll need to make sure that setting is set to true.

Further, you can configure SequenceStyleGenerator to prefer either a global sequence or sequence per entity if no name is given. This is controlled by a setting named prefer_sequence_per_entity

SequenceStyleGenerator is quite configurable in general. Have a look at its javadocs for more information: http://docs.jboss.org/hibernate/orm/4.1/javadocs/index.html?org/hibernate/id/enhanced/SequenceStyleGenerator.html

like image 169
Steve Ebersole Avatar answered Sep 18 '22 14:09

Steve Ebersole