[英]Hibernate not using sequence defined in Liquibase
I have defined a sequence in my liquibase
changelog, but looks like Hibernate
is ignoring it when inserting the entities. 我在liquibase
更改日志中定义了一个序列,但是看起来Hibernate
在插入实体时会忽略它。
Sequence defined in Liquibase looks like this. Liquibase中定义的序列如下所示。
<createSequence cycle="false" incrementBy="1"
startValue="1" maxValue="9223372036854775807" minValue="1"
sequenceName="seq_vehicle" />
And in the Entity class. 并在Entity类中。
@Entity
@Table(name = "VEHICLE")
public class Vehicle {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "VEHICLE_SEQ")
@SequenceGenerator(name = "VEHICLE_SEQ", sequenceName = "SEQ_VEHICLE")
private Long id;
I also added this property to hibernate.cfg.xml
我还将此属性添加到了hibernate.cfg.xml
<property name="hibernate.id.new_generator_mappings">false</property>
The problem is that whenever new entity is inserted to the DB, it just ignores Liquibase sequence, and starts from 50 and increments by 50 for any new insert. 问题在于,无论何时将新实体插入数据库,它都将忽略Liquibase序列,并且从50开始,对于任何新插入,以50递增。 How do I fix that? 我该如何解决?
Use @GenericGenerator
instead of @SequenceGenerator
. 使用@GenericGenerator
而不是@SequenceGenerator
。
@Id
@Column
@GenericGenerator(
name = "VEHICLE_SEQ",
strategy = "sequence",
parameters = {
@org.hibernate.annotations.Parameter(name = "sequence", value = "SEQ_VEHICLE")
}
)
@GeneratedValue(generator = "VEHICLE_SEQ")
private Long id;
The @SequenceGenerator
in your case hibernate's HILO mechanism which, by default, uses default allocation size of 50, and that's why you see gaps in id values id the database. @SequenceGenerator
在您的情况下是休眠的HILO机制,默认情况下使用默认分配大小50,这就是为什么您看到id值与数据库ID不一致的原因。
Just add allocationSize = 1
: 只需添加allocationSize = 1
:
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "VEHICLE_SEQ")
@SequenceGenerator(name = "VEHICLE_SEQ", sequenceName = "SEQ_VEHICLE", allocationSize = 1)
private Long id;
As @veljkost explained, the default value for the allocationSize
is 50 . 作为@veljkost解释,为默认值allocationSize
为50 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.