Issue with sequence why two entity are sharing the same sequence when generating schema with hbm2ddl ?
Asked Answered
J

3

9

I am using hbm2ddl in my hibernate based application to generate the db schema. The value of hibernate.hbm2ddl.auto property is create-drop.

I am using @Entity annotations for my POJO classes.

@Entity 
public class testTable1 {
     @Id     
     @GeneratedValue(strategy = GenerationType.SEQUENCE)
     Long id; 
}

@Entity 
public class testTable2 {
     @Id     
     @GeneratedValue(strategy = GenerationType.SEQUENCE)
     Long id; 
}

However on executing the code I keep getting continuously incremental Id values. e.g. for 2 tables the Id (i.e. Prim Key) should start each with 1. But after inserting records in Table 1, the sequence goes from next value for Table 2. It should start again from 1 for table 2. I tried GenerationType.SEQUENCE & GenerationType.AUTO. nothing works :-(

Jaquelynjaquenetta answered 29/7, 2011 at 19:30 Comment(0)
V
9

You are using the Global sequence generator that hibernate provide by default when no generator is provided as specifed by the JPA Spec. To have a private generator you should declare a private generator with the annotation @SequenceGenerator and set the generator attribute of the @GeneratedValue annotation

Extracted from javadoc

@GeneratedValue
(Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.

Defaults to the id generator supplied by persistence provider.

SequenceGenerator
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

Example:

@SequenceGenerator(name="EMP_SEQ", sequenceName="private_sequence")

Hibernate recommends that new projects use hibernate.id.new_generator_mappings=true as the new generators are more efficient and closer to the JPA 2 specification semantic

Section 1.3. Properties
2.2.3. Mapping identifier properties

Complete example

@Entity
@SequenceGenerator(name="PRIVATE_SEQ", sequenceName="private_sequence")
public class test {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PRIVATE_SEQ")
    Long id;
}
Venezuela answered 31/7, 2011 at 15:12 Comment(2)
True and I would like to add that if you want to set initial value and allocation size , you need to include hibernate.id.new_generator_mappings=true. Then you just need to use @SequenceGenerator like this : @SequenceGenerator(initialValue=1, allocationSize=1 , name="PRIVATE_SEQ", sequenceName="private_sequence") or whatever values you need to give.Moffit
@BreakingBenjamin can i use the same same sequence generator for multiple entity as you said above and get uniform exclusive id values in each entity? I mean each entity id will start with 1 and get increment by 1.Aquacade
P
1
 @Entity
    public class MyEntity2 {
        
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="myentity2_seq")
        @SequenceGenerator(name = "myentity2_seq",sequenceName = "myentity2_seq_table")
        private int id;

    @Column(length = 100)
    private String name;
//setters & getters ...
}

In the Database, it will create myentity2_seq_table Table for maintaining ids. we can customize table creation using initialValue = 100, allocationSize = 10 in SequenceGenerator.

Propaganda answered 12/12, 2020 at 6:8 Comment(0)
U
0

In Hibernate 5.2,

I had tried Joel Hudon's answer, but don't know why it doesn't work.

I found some shorter codes to solve it.

3.2. The annotated entity Java class

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public int id;

It could give out unique sequence number.

Hope it helps.

Uptodate answered 25/11, 2016 at 10:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.