0

I have a sequence in the DB like this

 CREATE SEQUENCE abc
        INCREMENT BY 1
        START WITH 1
 ;

I want a method like

getNextVal(); //My guess is I can fire select abc.nextval from dual

Which returns the next value from the sequence.

How should the .hbm.xml and the code for getNextVal look like. I am new to Hibernate , I tried digging up but have not been able to find a definitive answer.

Is the way of creating sequence optimal.

1 Answer 1

1

If you need sequence value to generate id, you don't have to worry about getNextVal() as it will be done for you based on your mapping with generated id. Some examples can be found here. And here is some info for annotations.

If you need sequence value for some other reason, you would have to do native SQL query. Something like this

Number val=session.createSQLQuery("select abc.nextval from dual").uniqueResult();
Sign up to request clarification or add additional context in comments.

4 Comments

All the examples tell me that the sequence is used inside a table. How would the mapping with just the sequence look like
As I mentioned, if you don't need it for id (which is most typical use case) then you can use native SQL as I described.
I am guessing that means no mapping is required, is that correct.
It may also be possible to use formula (docs.jboss.org/hibernate/core/3.3/reference/en/html/…). But I never tried that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.