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

Viewed 6879

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 :-(

3 Answers
 @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.

Related