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

24,045

Solution 1

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;

  ...
  ...

}

Solution 2

How is hibernate providing my id?

Well, you explicitly told the JPA engine to generate identifier automatically (with the @GeneratedValue annotation) using a strategy of type SEQUENCE indicating that a database sequence should be used to generate the identifier. In case you wonder, sequences are database specific objects (e.g. Oracle) that can be used to generate unique integers.

I see in my database there a single sequence named 'hibernate_sequence'

You didn't use the sequenceName annotation element in your @SequenceGenerator to specify the name of the database sequence object to use so Hibernate created a default sequence object during schema generation (which defaults to hibernate_sequence). To specify a sequence, do it like this:

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

Solution 3

In order to name the sequence you have to set the sequenceName in your @SequenceGenerator annotation:

@GeneratedValue(name="gen", strategy = GeneratorType.SEQUENCE)
@SequenceGenerator(name="gen", sequenceName="Sequence_Name", allocationSize = 1)
@Id
public Long getId()
{
    // ...
}

Of note, if you are using a pre-existing generator, your allocationSize must match the allocation size of that generator.

Share:
24,045
JavaRocky
Author by

JavaRocky

Software Engineer. "It the code is hot, we gonna kill it! :)" -- DaCav5 Geekstyle

Updated on May 16, 2020

Comments

  • JavaRocky
    JavaRocky about 4 years

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