7

I have a class mapped as an Entity to persist it in a database. I have an id field as the primary key so every time the object is persisted the value of the id is retrieved from the sequence "myClass_pk_seq", a code like the following one.

@Entity @Table(name="myObjects") public class MyClass { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence") @SequenceGenerator(name="sequence", sequenceName="myClass_pk_seq", allocationSize=1) @Column(name="myClassId") private Integer id; private Integer code; ... } 

I need to make in "code" attribute something similar to id. I need to have a sequence so I can assign to code the next value of the sequence (to reserve that value in case the user wants to reserve it but without persisting data). I mean the user will see the field, if he doesn't know what to enter he can push a button and receieve in the screen the next possible value (and he can or not accept it). How can I get the next value of a sequence defined in JPA (and increment its value) without persisting the data for a non primary key field?

I just want to have a method which call nextval on a sequence associated with "code" field, and returns the value. What's the best way to do it in JPA with annotations?

Thanks.

3
  • why do you need to "reserve" it. Why not let JPA handle that Commented Jun 18, 2010 at 8:10
  • @Bozho ok, I've completely change my question so now it makes more sense. Commented Jun 18, 2010 at 9:00
  • see also stackoverflow.com/questions/277630/… Commented Sep 15, 2011 at 15:00

2 Answers 2

4

I just want to have a method which call nextval on a sequence associated with "code" field, and returns the value. What's the best way to do it in JPA with annotations?

  • Use native SQL to get the next sequence value when the user pushes the button. Either create the sequence manually or use a "fake entity" to have JPA create it for you.
  • If you don't want to use native SQL, insert an entity relying on the sequence and gets its id.

Both solutions sounds a bit ugly. Maybe you could simply use a random generator like a UUID generator.

Actually, you didn't mention anything about the uniqueness of the code (and the JPA annotations don't show it must be unique). Why don't you return a random int?

Sign up to request clarification or add additional context in comments.

4 Comments

Code number must be unique but not always. When the button is pushed a new unique value must be given though the user can change it and insert another value (which can be even one already stored in database). It's like giving the oportunity to the user to see the next possible value and he can or not accept it (but without saving anything in database)
@Javi Then why not using a random generator?
maybe it could do the trick,or just even a static long attribute and increment it by one.
it hurts that JPA does not support such a primitive standard database construct as sequences. Thanks for clearing this up!
-1

See another question/answers on the subject of using sequence defined elsewhere than id fields. You can create a fake entity with one field (of type Long id). Connect it to the sequence you defined in the DB. Then create a CrudRepository implementation for that entity and call its save() method with an empty instance of the fake entity object you defined. Hibernate will run for you a "select YOUR_SEQ.NEXTVAL from dual" query.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.