开发者

using oracle sequences with jpa (toplink)

开发者 https://www.devze.com 2023-04-07 10:31 出处:网络
I have a sequence object in my oracle db: create sequence BASE_SEQ minvalue 1 maxvalue 9999999999999999999999999999

I have a sequence object in my oracle db:

create sequence BASE_SEQ
minvalue 1
maxvalue 9999999999999999999999999999
start with 100
increment by 1
nocache;

I us开发者_如何学Ce jpa(toplink) for my web application. I have base class for all my db objects:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

public class AbstractEntity implements Serializable {
    protected BigDecimal id;

    @javax.persistence.Column(name = "ID")
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BASE_SEQ")
    @SequenceGenerator(name="BASE_SEQ",sequenceName="BASE_SEQ", catalog = "DOF")
    public BigDecimal getId() {
        return id;
    }

This class is inherited by some entities. After I start my application, and persist/merge several entities to db, i can find, that theirs' PK starts with 51 (instead of expected 100).

After that, I go to my db, view DDL of my sequence object and see, that it has been changed to:

create sequence BASE_SEQ
minvalue 1
maxvalue 9999999999999999999999999999
start with 101
increment by 1
nocache;

Why so happens? I have some entities with PK 51, 52 ... etc, and sequence starting with 101.

AS - GlassFish 3.1.1


The default preallocationSize on SequenceGenerator is 50, it must match your sequence increment, which you have set to 1.

Either change your increment to 50 (recommended), or change your preallocationSize to 1 (this will result in poor insert performance).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号