Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top