EclipseLink和序列生成器预分配 [英] EclipseLink and Sequence Generator Preallocation

查看:85
本文介绍了EclipseLink和序列生成器预分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我无法摆脱困境。在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,则会出现此错误:

 名为[EMAIL_SEQ_GEN]的序列安装不正确。其增量与预分配大小不匹配。 

所以我当然会在谷歌周围看到一些关于EclipseLink创建一个负数的初始值为1并且它应该等于allocationSize。所以,加入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是不可能的。

解决方案

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

  public @interface SequenceGenerator {
/ **
*(可选)序列对象
*开始产生的值。
* /
int initialValue()default 1;

/ **
*(可选)从序列中分配
*序号时的增加量。
* /
int allocationSize()默认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(默认值)

CREATE SEQUENCE EMAIL_SEQ_GEN以1增加500开始;

产生

  entityId = 1  -  500 + 1 = -500 // EclipseLink错误






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

CREATE SEQUENCE EMAIL_SEQ_GEN以1增加500开始;

产生

  entityId = 1  -  500 + 1 = -500 // EclipseLink错误






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


CREATE SEQUENCE EMAIL_SEQ_GEN以500增加500开始;

产生

  entityId = 500  -  500 + 500 = 500 //罚款,但不适用
entityId = 500 - 500 + 1000 = 1000 //增加500
entityId = 500 - 500 + 1500 = 1500 //增加500
...






为了满足您的要求,应该使用以下内容:

  @SequenceGenerator(
name =email-seq -gen,
sequenceName =EMAIL_SEQ_GEN,
allocationSize = 500
)// initialValue = 1(默认),但是'START WITH'= 500

CREATE SEQUENCE EMAIL_SEQ_GEN以500增加1开始;

产生

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






使用以下SQL可以从底层数据库中删除现有序列命令

  DROP SEQUENCE email_seq_gen RESTRICT; 

我希望它有帮助。


I have an issue I can't get my head around. In hibernate I have no problem with the following:

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

Then in my schema.ddl I have this:

CREATE SEQUENCE EMAIL_SEQ_GEN START 1 INCREMENT 500;

Not much to see here. Everything works as expected. However, if I switch my provider to EclipseLink I get this error:

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

So of course I google around and see something about EclipseLink creating a negative number if the initial value is 1 and that it should equal the allocationSize.

So, okay, so adding "initialValue=500" and updating my DDL scripts to "START 500" fixes this but now my numbering starts at 500 instead of 1. What gives? Is this an EclipseLink bug or is there something I am not understanding. I would like to generate sequences that start at 1 and have allocation sizes that are tuned to the entity (in this case 500). How would I do that with EclipseLink?

Thanks!

Another way to ask this is....given this DDL:

CREATE SEQUENCE EMAIL_SEQ_GEN START 1 INCREMENT 500;

What is the correct way to annotate my entity to use it with EclipseLink?

If I let EclipseLink generate my DDL then this:

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

Will generate this:

CREATE SEQUENCE EMAIL_SEQ_GEN INCREMENT BY 500 START WITH 500;

Which kind of implies that it is IMPOSSIBLE to create a DDL with a "START WITH 1" using EclipseLink.

解决方案

By default entities annotated with @SequenceGenerator use initialValue=1 and 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;
}

A "sequential" entity id seems to be calculated by EclipseLink with the following formula:

entityId = initialValue - allocationSize + INCREMENT_BY

or in case of using DDL:

entityId = START_WITH - allocationSize + INCREMENT_BY

Going back to your particular cases:


@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;

produces

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;

produces

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;

produces

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


To meet your requirements the following one should be used:

@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;

produces

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


An existing sequence can be removed from the underlying database with the following SQL command:

DROP SEQUENCE email_seq_gen RESTRICT;

I hope it helps.

这篇关于EclipseLink和序列生成器预分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆