如何在JPA 2/Hibernate中生成可移植的和本机ID? [英] How to make portable AND native ID generation in JPA 2 / Hibernate?

查看:75
本文介绍了如何在JPA 2/Hibernate中生成可移植的和本机ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在目前运行Hibernate和MySQL的JPA 2实体上具有本机和可移植ID生成

I would like to have native and portable id generation on my JPA 2 entities, currently running Hibernate and MySQL

在使用@GeneratedValue(strategy = AUTO)时,hibernate默认为MySQL上的"hibernate_sequence"表,我希望IDENTITY

When using @GeneratedValue(strategy=AUTO), hibernate defaults to the "hibernate_sequence" table on MySQL, i would like IDENTITY

如果我使用@GeneratedValue(strategy = IDENTITY)解决它,我将失去Oracle/Postgres的可移植性

If i solve it using @GeneratedValue(strategy=IDENTITY), i loose Oracle/Postgres portability

当@GeneratedValue strategy = AUTO时,如何将Hibernate设置为使用IDENTITY作为mysql的默认值?

How can i set Hibernate to use IDENTITY as default for mysql when @GeneratedValue strategy=AUTO?

推荐答案

在Hibernate中无需更改代码,就不会影响它.一种解决方法是为具有不同id要求的每个数据库使用不同的映射集(xml而不是注释).这当然是很多事.

Without code change in Hibernate you cannot affect that. One way around is using different set of mappings (xml instead of annotations) for each database with different id requirements. That is of course quite much to do.

唯一独立于数据库供应商的可移植方式是使用TABLE策略生成ID.

Only truly portable way that works independently from database vendor, is to generate id with TABLE strategy.

这篇关于如何在JPA 2/Hibernate中生成可移植的和本机ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆