NDesai
NDesai

Reputation: 2131

H2 Sequence is generating Negative No. in Column [JPA Spring]

I am trying to insert into H2 Database tables where there are two tables

  1. Parent(ID,Name)
  2. Child(ID,ParentID,Name)

I already defined the sequence script for this two tables.

  1. Create sequence parent_seq start with 1 increment by 1;
  2. Create sequence child_seq start with 1 increment by 1;

I have to bean classes for Parent and Child

Root:

@Entity
public class Root{

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="abstract_generator")
private Long id;

//getter setter for Id
}

Parent:

@Entity
@Access(AccessType.FIELD)
@Table(name="PARENT")
@SequenceGenerator(name="abstract_generator",sequenceName="parent_seq")
@NamedQuery(name="parent.findAll",query="select q from parent q")
public class Parent extends Root{

@Column(name="Name")
private String name;

public void setName(String name){
this.name=name;
}
}

Child:

    @Entity
    @Access(AccessType.FIELD)
    @Table(name="CHILD")
    @SequenceGenerator(name="abstract_generator",sequenceName="child_seq")
    @NamedQuery(name="child.findAll",query="select q from child q")
    public class child extends Root{

    @Column(name="Name")
    private String name;

    @Column(name="ParentId")
    private Long parentId;

    //getter setter for Parentid and name
}

My parent and child classes are fetching the id from root and root class is generating the id for each class.

Now problem is I am getting negative value when system is trying to insert the value of Id.

Result of Entry:

Parent

id Name

1 abc

-98 kjd

-97 djhf

-96 djh

I just wanted to know that why it is entering negative value. I can not find the reason. I debugged the whole code but still struggling to find out the reason.

Upvotes: 1

Views: 3919

Answers (1)

NDesai
NDesai

Reputation: 2131

It was happening because the JPA Properties didn't get sync with database sequence.

So first map with JPA hibernate:

Properties prop=new Properties();
prop.put("hibernate.dialect","H2");
prop.put("hibernate.show_sql","true");
prop.put("hibernate.format_sql","true");
prop.put("hibernate.jdbc.batch_size",batchSize);
prop.put("hibernatae.order_inserts","true");
prop.put("hibernatae.order_updates","true");
prop.put("hibernate.jdbc.batch_versioned_data","true");
prop.put("hibernate.id.new_generator_mappings","true");

Second set allocationSize so it can sync with sequence:

CREATE SEQUENCE parent_seq START WITH 1 INCREMENT BY 10;

so set allocationSize with your bean/Model class

Parent:

@Entity
@Access(AccessType.FIELD)
@Table(name="PARENT")
@SequenceGenerator(name="abstract_generator",sequenceName="parent_seq",allocationSize=10)
@NamedQuery(name="parent.findAll",query="select q from parent q")
public class Parent extends Root{

@Column(name="Name")
private String name;

public void setName(String name){
this.name=name;
}
}

My problem was hibernate mapping was not correctly sync with database. That's why some random negative number are coming as primary key in database.

Upvotes: 2

Related Questions