EclipseLink和序列生成器预分配

时间:2013-08-27 19:39:03

标签: hibernate jpa eclipselink

我有一个问题我无法理解。在休眠中,我对以下内容没有任何问题:

@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并且它应该等于allocationSize,那么我可以浏览一下有关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是不可能的。

3 个答案:

答案 0 :(得分:19)

默认使用@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;
}

“顺序”实体ID似乎由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 command从基础数据库中删除现有序列:

DROP SEQUENCE email_seq_gen RESTRICT;

我希望它有所帮助。

答案 1 :(得分:2)

你的序列应该从500开始而不是1.如果从1开始,那么第一个nextval只会给你1个id而不是500个。

否则在设置之后调用序列的nextval,所以它到达501。

错误是警告还是错误?如果你忽略它,它仍然有用吗?

答案 2 :(得分:1)

我可以通过在模式生成中添加一个句子来解决此问题: RESTART

实体定义

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=Secuences.PROFILES_SEQ)
@SequenceGenerator(name=Secuences.PROFILES_SEQ, sequenceName=Secuences.PROFILES_SEQ, allocationSize=25)
@Column( name = "id" )
private Long internalId;

SQL模式初始化

-- create sequence
CREATE SEQUENCE PROFILES_SEQ START WITH 1;

-- use sequence to set values in column
UPDATE public.cc_user_profile SET id = nextval('PROFILES_SEQ');

-- update sequence
ALTER SEQUENCE PROFILES_SEQ INCREMENT BY 25 START WITH 25;
ALTER SEQUENCE PROFILES_SEQ RESTART;

希望它可以帮助某人。