2

My target is that EJB should generate a sequence as it generates tables from Entities. How can I do that?

I thought about this, but in that case I only use an existing sequence. I'd like a sequence to be generated by JPA. I think this isn't possible, or is it?

@Entity
@Table(name = "CUSTOMER")
@SequenceGenerator(name = "sb_sequence",sequenceName = "sb_sequence", initialValue=5420)
public class Customer extends EntityBase
{
    private static final long serialVersionUID = 3456353535358L;

    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sb_sequence")
    @Column(name = "CUS_SEQUENCE")
    private long cusSequence;
.
.
.
}

Background: Before I insert the first value into database, I need to retrieve the first sequence number (probably with select nextval('sequence'); )

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • This really seems like it's going to depend not on JPA but on your JPA implementation to generate the DDL. That said, if you want to know what the ID for the object will be before the object is persisted to the store, you may need to handle calling the sequence yourself. Running `select nextval('sequence');` before saving the object with `GenerationType.SEQUENCE` will actually result in TWO sequence numbers being generated. Also see [this related question](http://stackoverflow.com/questions/3067986/calling-next-value-of-a-sequence-in-jpa). – ig0774 Mar 06 '12 at 22:31

1 Answers1

0

Most JPA provider allow the DDL to be generated, but this is not part of the JPA spec.

For EclipseLink see,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL

James
  • 17,965
  • 11
  • 91
  • 146