Wednesday, February 27, 2013

Eclipselink Postgres GeneratedValue strategy=Identity not compatible with HistoryPolicy

I wanted to do this in my Eclipselink/Postgres application:
       
        TABLE
    -------------------
    ID SERIAL NOT NULL

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column

    private long id;

This produces exceptions upon persist().   I think it's this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=347539

 Solution is to do this

        TABLE
    -------------------
    ID SERIAL NOT NULL

 It's important to know that SERIAL implicitly creates a sequence named TABLE_ID_SEQ.  So I can use strategy=SEQUENCE to use it:

    @Id
    @SequenceGenerator(name="table_id_seq", allocationSize=1)
    @GeneratedValue(strategy=SEQUENCE, generator="table_id_seq")
    @Column
    private long id;


It's important to include allocationSize=1, otherwise Eclipselink will start using nextval() - 50 as the next ID... producing Primary Key conflicts upon persist.

 

No comments:

Post a Comment