tags:

views:

56

answers:

4

Hello,
I wonder is there a clean (or native) way to generate sequence for a table, to use it as a serial number. It needs to be consecutive (1, 2, 3 ...etc), and avoid any possible race/transaction issues(if multiple user try to persist same time). It doesn't require to be the primary key.

   @Id
    private Long id;

    private Long serialNumber;

thanks.

A: 

@id @GeneratedValue(strategy=IDENTITY) ?

Julio Faerman
The OP is looking for a solution for a non `Id` field.
Pascal Thivent
A: 

@SequenceGenerator

http://wiki.eclipse.org/EclipseLink/Examples/JPA/PrimaryKey#Using_Sequence_Objects

reverendgreen
The OP is looking for a solution for a non `Id` field.
Pascal Thivent
+1  A: 

I wonder is there a clean (or native) way to generate sequence for a table, to use it as a serial number.

To my knowledge, no, not for a non Id field.

But you could use a dedicated entity with the appropriate generator strategy and persist a new instance when required to get the next id from it.

Another option would be to simulate a TABLE generator strategy and to use native SQL to read the next id from it and increment it.

Pascal Thivent
I guess the second option is straight forward, provided the transaction is ensured (if many concurrent users try to create the same entity).
bsreekanth
@bsreekanth: Yes, I second this (and this is very likely what I would implement).
Pascal Thivent
As you commented below, I was looking for a sol for non id field. an example, say we have 2 customers, and need customer_request_id of each customer to be contigous. Eg. Intel_1, Intel_2... ,IBM_1, IBM_2.. .
bsreekanth
@bsreekanth: You can tweak the "TABLE-like" generator strategy to use multiple lines (depending on the customer), concatenate a special key, etc. You can make this as flexible as you want.
Pascal Thivent
+1  A: 

DataNucleus supports use of @GeneratedValue on non Id fields.

DataNucleus
thank you..that is a neat feature.. unfortunately, I use a hibernate based system.
bsreekanth