小编典典

EclipseLink和Sequence Generator预分配

hibernate

我有一个问题,我无法解决。在hibernate状态下,我没有以下问题:

@GeneratedValue( strategy = GenerationType.AUTO, generator = "email-seq-gen" )
@SequenceGenerator( name="email-seq-gen", sequenceName="EMAIL_SEQ_GEN", allocationSize=500 )

然后在我的schema.ddl中,我有这个:

CREATE SEQUENCE EMAIL_SEQ_GEN START 1 INCREMENT 500;

这里没什么可看的。一切正常。但是,如果我将提供程序切换到EclipseLink,则会出现此错误:

The sequence named [EMAIL_SEQ_GEN] is setup incorrectly.  Its increment does not match its pre-allocation size.

因此,我当然会四处搜索,如果初始值为1且它应该等于分配大小,那么我会看到有关EclipseLink创建负数的一些信息。

因此,好的,因此添加“ initialValue = 500”并将DDL脚本更新为“ START
500”即可解决此问题,但是现在我的编号从500开始而不是1。这是EclipseLink错误还是我不了解的东西?我想生成从1开始并具有调整至实体(在这种情况下为500)的分配大小的序列。我该如何使用EclipseLink?

谢谢!

提出此问题的另一种方法是....鉴于此DDL:

CREATE SEQUENCE EMAIL_SEQ_GEN START 1 INCREMENT 500;

注释我的实体以将其与EclipseLink一起使用的正确方法是什么?

如果我让EclipseLink生成我的DDL,那么这是:

@GeneratedValue( strategy = GenerationType.AUTO, generator = "email-seq-gen" )
@SequenceGenerator( name="email-seq-gen", sequenceName="EMAIL_SEQ_GEN", initialValue=1, allocationSize=500 )

将生成此:

CREATE SEQUENCE EMAIL_SEQ_GEN INCREMENT BY 500 START WITH 500;

哪种暗示无法使用EclipseLink创建带有“ START WITH 1”的DDL。


阅读 366

收藏
2020-06-20

共1个答案

小编典典

默认情况下,@SequenceGenerator使用initialValue = 1和alocationSize = 50 注释的实体。

public @interface SequenceGenerator {
    /** 
     * (Optional) The value from which the sequence object 
     * is to start generating.
     */
    int initialValue() default 1;

    /**
     * (Optional) The amount to increment by when allocating 
     * sequence numbers from the sequence.
     */
    int allocationSize() default 50;
}

EclipseLink似乎使用以下公式来计算“顺序”实体标识:

entityId = initialValue - allocationSize + INCREMENT_BY

或使用DDL:

entityId = START_WITH - allocationSize + INCREMENT_BY

回到您的特殊情况:


@SequenceGenerator( 
    name="email-seq-gen", 
    sequenceName="EMAIL_SEQ_GEN", 
    allocationSize=500
) // initialValue=1 (default)

CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 1 INCREMENT BY 500;

产生

entityId = 1 - 500 + 1 = -500 // EclipseLink error

@SequenceGenerator( 
    name="email-seq-gen", 
    sequenceName="EMAIL_SEQ_GEN", 
    initialValue=1, 
    allocationSize=500 )

CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 1 INCREMENT BY 500;

产生

entityId = 1 - 500 + 1 = -500 // EclipseLink error

@SequenceGenerator( 
    name="email-seq-gen", 
    sequenceName="EMAIL_SEQ_GEN", 
    initialValue=500, 
    allocationSize=500
)

CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 500 INCREMENT BY 500;

产生

entityId = 500 - 500 + 500 = 500 // fine, but inappropriate
entityId = 500 - 500 + 1000 = 1000 // incremented by 500
entityId = 500 - 500 + 1500 = 1500 // incremented by 500
...

为了满足您的要求,应使用以下一种:

@SequenceGenerator( 
    name="email-seq-gen", 
    sequenceName="EMAIL_SEQ_GEN", 
    allocationSize=500 
) // initialValue=1 (default) but 'START WITH'=500

CREATE SEQUENCE EMAIL_SEQ_GEN START WITH 500 INCREMENT BY 1;

产生

entityId = 500 - 500 + 1 = 1
entityId = 500 - 500 + 2 = 2
entityId = 500 - 500 + 3 = 3
...

可以使用以下SQL
命令从基础数据库中删除现有序列:

DROP SEQUENCE email_seq_gen RESTRICT;

希望对您有所帮助。

2020-06-20