kal
kal

Reputation: 29371

Hiberate mapping and java code for a database sequence

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.

Upvotes: 0

Views: 367

Answers (1)

Alex Gitelman
Alex Gitelman

Reputation: 24722

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();

Upvotes: 1

Related Questions