问题描述
我试图插入到H2数据库表中,其中有两个表
- 父(ID,名称)
- 子(ID,ParentID,Name)
我已经为这两个表定义了序列脚本。 p>
- 创建序列 parent_seq 以1递增开始;
- 创建序列 child_seq 以1递增1开始;
我必须为Parent和Child创建bean类
> 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;
小孩: (b)b
$ b $ pre $ @Entity
@Access(AccessType.FIELD)
@Table(name =CHILD)
@SequenceGenerator(name =abstract_generator,sequenceName =child_seq)
@NamedQuery(name =child.findAll,query =从q中选择q)
public class child extends根{
@Column(name =Name)
私有字符串名称;
@Column(name =ParentId)
private长parentId;
//用于Parentid和名称
的$ getter setter
我的父类和子类从root获取id,root类为每个类生成id。
现在问题是系统尝试插入Id的值。
结果:
父
id 姓名
-98 kjd
-97 djhf
-96 djh
我只是想知道它为什么会输入负值。我找不到原因。我调试了整个代码,但仍然努力找出原因。
这是因为JPA属性没有得到与数据库序列同步。
所以先用JPA hibernate映射:
属性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);
第二组分配大小,因此它可以与序列同步:
CREATE SEQUENCE parent_seq START WITH 1 INCREMENT BY 10;
所以用你的bean / Model类设置allocationSize
@Entity
@Access(AccessType.FIELD)
@Table(name =PARENT)
@SequenceGenerator(name =abstract_generator,sequenceName =parent_seq,allocationSize = 10)
@NamedQuery(name =parent.findAll, )
public class Parent extends Root {
@Column(name =Name)
private String name;
public void setName(String name){
this.name = name;
$ b我的问题是hibernate映射与数据库不能正确同步。这就是为什么一些随机的负数将成为数据库中的主键。
I am trying to insert into H2 Database tables where there are two tables
- Parent(ID,Name)
- Child(ID,ParentID,Name)
I already defined the sequence script for this two tables.
- Create sequence parent_seq start with 1 increment by 1;
- 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.
解决方案 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.
这篇关于H2序列在[JPA Spring]列中生成负号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!