Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)

Tags:

I'm usign @GeneratedValue(strategy = GenerationType.AUTO) to generate the ID on my entity.

I don't now how it works, but on my child table, generates ID values, that follow the parent sequence.

//parent table @Entity @Table (name = "parent") public class Parent {      @Id     @GeneratedValue (strategy = GenerationType.AUTO)     @Column (name = "id")     private long id;       @OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)     @JoinColumn (name = "parentId")     @ForeignKey (name = "FKparent")     private List<child> child;  }  //child table @Entity @Table (name = "child") public class Child {      @Id     @GeneratedValue (strategy = GenerationType.AUTO)     @Column (name = "id")     private long id; } 

The inserted ID values on parent, updates the sequence. The inserted ID values on child, updates the sequence. On the next insert of parent, the sequence... uses values updated by child insertions...

This Annotations, aren't creating two sequences, only one. Is this correct/expected?

I inserted my entities with my DAO service only using entityManager.persist(parent);

like image 985
Moli Avatar asked Jun 18 '10 10:06

Moli


People also ask

What is @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.

How does GenerationType auto work?

GenerationType.AUTOAUTO is the default generation type and lets the persistence provider choose the generation strategy. private Long id; If you use Hibernate as your persistence provider, it selects a generation strategy based on the database specific dialect. For most popular databases, it selects GenerationType.

What is GenerationType in Hibernate?

SEQUENCE is the generation type recommended by the Hibernate documentation. The generated values are unique per sequence. If we don't specify a sequence name, Hibernate will reuse the same hibernate_sequence for different types.

What is GenerationType sequence?

GenerationType. SEQUENCE in hibernate generates the sequence for the primary column of the table. We need to create a sequence generator in database and refer that name in the code. The syntax of GenerationType.


2 Answers

These Annotations are no creating two sequences, only one. Is this correct/expected?

That's the expected behavior. When using @GeneratedValue(strategy = GenerationType.AUTO), the JPA provider will pick an appropriate strategy for the particular database. In the case of Oracle, this will be SEQUENCE and, since you did not specify anything, Hibernate will use a single global sequence called hibernate_sequence.

Is this correct? Well, I don't know, it depends on your needs. Just in case, the default maximum value for an Oracle sequence is 1E+27, or 1,000,000,000,000,000,000,000,000,000. That's enough for many.

Now, it is possible to use GenerationType.AUTO and still control the name of the sequence when the database uses sequences:

@Id @GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen") @SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ") private Long id; 
like image 162
Pascal Thivent Avatar answered Dec 12 '22 21:12

Pascal Thivent


Yes this is correct and expected.

You can create individual sequences for each table, but IMHO that is just extra code with no actual benefit.

like image 32
Jens Schauder Avatar answered Dec 12 '22 20:12

Jens Schauder