egaga
egaga

Reputation: 21722

How to create and read a sequence in Hibernate?

I need to use a sequence to get a unique value. The production code uses postgres but I would like to access it via Hibernate so that I can test this with HSQLDB.

How can I create and read a sequence in Hibernate?

Upvotes: 1

Views: 967

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328604

Have a look at Dialect (https://github.com/hibernate/hibernate-orm/blob/5.0/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java).

It contains code to determine whether your database supports sequences (supportsSequences()) and methods to create and drop them ('getCreateSequenceString(String sequenceName)').

Unfortunately, there is no clean way to get the dialect from the session; see How to get Hibernate dialect during runtime

[OLD Answer]

You could try to reuse the code from HibernateDialect but the main API doesn't offer this. Your best bet is probably to wrap this in an interface and then use different implementations depending on your dialect.

Upvotes: 2

Related Questions