質問

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 use 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).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top