先声明一下我用的框架是Spring + Hibernate + SpringMVC 数据库使用的是Oracle
昨天遇到了一个特诡异的问题就是我使用Oracle序列,把主键的计数交给Hibernate处理,
@
写完部署什么都没问题,可当我写了测试类进行测试时发现主键的初始值竟然是50,其步长亦是50,在同事的帮助下发现原来是Hibernate在做鬼,@SequenceGenerator中添加两个参数(allocationSize = 1, initialValue = 1)就OK。通过查找Hibernate的资料发现原来是因为allocationSize的默认值是50.具体请参考http://www.oracle.com/technology/global/cn/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#SequenceGenerator
昨天遇到了一个特诡异的问题就是我使用Oracle序列,把主键的计数交给Hibernate处理,
@
Entity
@Table(name = "BIO_STUDY")
public class Study implements Serializable{
private static final long serialVersionUID = -5932941248053882057L;
private int id;
private Project project;
private String name;
private String description;
private Set<Experiment> experiments;
@Id
@Column(name = "ID")
@SequenceGenerator(name = "BIO_STUDY_SQ",
sequenceName = "BIO_STUDY_SQ" )
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BIO_STUDY_SQ")
public int getId() {
return id;
}
写完部署什么都没问题,可当我写了测试类进行测试时发现主键的初始值竟然是50,其步长亦是50,在同事的帮助下发现原来是Hibernate在做鬼,@SequenceGenerator中添加两个参数(allocationSize = 1, initialValue = 1)就OK。通过查找Hibernate的资料发现原来是因为allocationSize的默认值是50.具体请参考http://www.oracle.com/technology/global/cn/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#SequenceGenerator