Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is my id being generated with JPA using Hibernate with the Oracle 10g dialect?

I have some code:

@Id
@SequenceGenerator(name = "SOMETHING_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ")
@Column(name = "SOMETHING", nullable = false)
private Long id;

How is hibernate providing my id?

I see in my database there a single sequence named 'hibernate_sequence' and no other hibernate 'special tables'.

like image 822
JavaRocky Avatar asked Mar 05 '10 03:03

JavaRocky


People also ask

How does Hibernate sequence generator work?

AUTO: Hibernate selects the generation strategy based on the used dialect, IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

Does Oracle support GenerationType identity?

Hibernate in Practice - The Complete CourseOracle 11g does not support identity key generator. This feature is supported in Oracle 12c. GenerationType. SEQUENCE − In sequence, we first ask database for the next set of the sequence then we insert row with return sequence id.

What is the use of ID GeneratedValue strategy GenerationType identity?

GenerationType lets us define that strategy. Here @GeneratedValue(stratergy=GenerationType. IDENTITY) is telling our DB to store primary key in the identity column which is a default column in SQL for default auto incremented primary key generation.

What is generated value in Hibernate?

Provides 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.


1 Answers

Actually, here your SOMETHING_SEQ is the name of sequence you configured somewhere in your hibernate config. And hibernate_sequence is the sequence name in the database. In configuration it would be looking something like below,

<sequence-generator name="SOMETHING_SEQ" 
    sequence-name="hibernate_sequence"
    allocation-size="<any_number_value>"/>

You can completely skip this configuration by using annotation instead. Then your @SequenceGenerator annotation would need to provide few more paramters. Below is the example.

@SequenceGenerator(name="SOMETHING_SEQ", sequenceName="hibernate_sequence", allocationSize=10)

For example multiple entity classes would do something like below,

@Entity
public class Entity1 {
  @Id
  @SequenceGenerator(name = "entity1Seq", sequenceName="ENTITY1_SEQ", allocationSize=1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity1Seq")
  @Column(name = "ID", nullable = false)
  private Long id;

  ...
  ...

}

@Entity
public class Entity2 {
  @Id
  @SequenceGenerator(name = "entity2Seq", sequenceName="ENTITY2_SEQ", allocationSize=10)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity2Seq")
  @Column(name = "ID", nullable = false)
  private Long id;

  ...
  ...

}
like image 143
Adeel Ansari Avatar answered Sep 28 '22 02:09

Adeel Ansari