users@glassfish.java.net

Re: [Fwd: Use of SERIAL and BIGSERIAL w/ EJB3 and POSTGRES]

From: <glassfish_at_javadesktop.org>
Date: Sun, 05 Aug 2007 08:44:35 PDT

SERIAL in Postgresql is just a shortcut for

int pk default nextval('table_name.id_seq');

so you have 2 choices, either switch the table definition of the column to SERIAL which will create the sequence for you or just create the sequence yourself. Either way you need to modify your entity class to set the pk using the sequence with something like this:

[i] @Id
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TTT_SEQUENCE_GENERATOR")
        @SequenceGenerator(name="TTT_SEQUENCE_GENERATOR", sequenceName="seq_name", allocationSize=1)
        @Column(name = "id", nullable = false)
        private int requestID;
[/i]

where [i]TTT_SEQUENCE_GENERATOR[/i] is a unique name for the sequence within glassfish and [i]seq_name[/i] is the name of the sequence in the database.

Now whenever you create an entity, glassfish will initialise the pk using the sequence.

If you already have data in the table remember to set the sequence start value to 1 after the largest value in the db (to prevent clashes)

I'm guessing BIGSERIAL is the same except as SERIAL except uses a bigint to store the value in the table so you would need to change the entity type to match in this case

Hope that helps.
[Message sent by forum member 'jsl123' (jsl123)]

http://forums.java.net/jive/thread.jspa?messageID=229507