开发者

How to create and read a sequence in Hibernate?

开发者 https://www.devze.com 2022-12-09 17:25 出处:网络
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 H开发者_如何学JAVASQLDB.

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 H开发者_如何学JAVASQLDB.

How can I create and read a sequence in Hibernate?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号