How is my ID created using JPA using Hibernate with Oracle 10g dialect?

I have a code:

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

How does hibernate provide my identifier?

In my database, I see one sequence named "hibernate_sequence" and other special "hibernate" tables.

+7
java oracle10g orm hibernate jpa
source share
4 answers

Actually, here your SOMETHING_SEQ is the name of the sequence that you configured somewhere in your hibernate configuration. And hibernate_sequence is the name of the sequence in the database. In the configuration, it will look something like this:

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

You can skip this configuration completely by using annotation instead. Then your @SequenceGenerator annotation should provide several parameters. The following is an example.

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

For example, several entity classes will 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; ... ... } 
+9
source share

To name a sequence, you must set sequenceName to the @SequenceGenerator annotation:

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

It should be noted that if you are using an existing generator, your allocationSize should match the size of the distribution of this generator.

+4
source share

How does hibernate provide my identifier?

Well, you explicitly told the JPA engine to generate the identifier automatically (with the @GeneratedValue annotation), using a strategy like SEQUENCE indicates that the database sequence should be used to generate the identifier. In case you're interested, sequences are database-specific objects (like Oracle) that can be used to generate unique integers.

In my database, I see one sequence named "hibernate_sequence"

You did not use the @SequenceGenerator annotation sequenceName in your @SequenceGenerator to indicate the name of the database sequence object to use in this way Hibernate created the default sequence object when generating the schema (which defaults to hibernate_sequence ). To specify a sequence, do the following:

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

In Oracle, you do not have the auto_increment type, as in MySQL. So, to create an auto_increment column, you need to use a sequence.

This is an example of how you can achieve this.

 create table test (id number, testdata varchar2(255)); create sequence test_seq start with 1 increment by 1 nomaxvalue; create trigger test_trigger before insert on test for each row begin select test_seq.nextval into :new.id from dual; end; 

So, you create a sequence and start a trigger before adding each line.

So hibernate should do something like this or instead of using a trigger executing

 insert into test values(test_seq.nextval, 'no trigger needed!'); 

Note: Example from here.

0
source share

All Articles