简体   繁体   English

Hibernate 5 ID AUTO Generation Type for Oracle as Sequence and MySQL as Identity

[英]Hibernate 5 ID AUTO Generation Type for Oracle as Sequence and MySQL as Identity

We have a Java program using Hibernate 5 that can be installed using different DB Engines: Oracle, PostgreSQL or SQL Server.我们有一个使用 Hibernate 5 的 Java 程序,可以使用不同的数据库引擎进行安装:Oracle、PostgreSQL 或 SQL Server。 Now we are introducing MySql.现在我们介绍MySql。

The entities have a Generated Value AUTO identifier.实体具有生成值 AUTO标识符。

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;

This worked fine until now.直到现在这工作正常。 Oracle and PostgreSQL installations are using HIBERNATE_SEQUENCE sequence to generate the id values and SQL Server is using a table generated id (IDENTITY). Oracle 和 PostgreSQL 安装使用 HIBERNATE_SEQUENCE 序列来生成 id 值,而 SQL Server 使用表生成的 id (IDENTITY)。

With MySQL we would like to use also the table generated id, but Hibernate 5 GenerationType.AUTO on MySQL tries the TABLE generator instead of the IDENTITY generator.对于 MySQL,我们还想使用表生成的 id,但 MySQL 上的 Hibernate 5 GenerationType.AUTO 尝试使用 TABLE 生成器而不是 IDENTITY 生成器。

I found this solution by Vlad Mihalcea where using a native generator makes Hibernate to select IDENTITY for MySQL:我找到了 Vlad Mihalcea 的这个解决方案,其中使用本机生成器使 Hibernate 为 MySQL 选择 IDENTITY:

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
protected Long id;

That's nice for MySQL, but then the application is no longer working in Oracle (ORA-02289: sequence does not exist).这对 MySQL 来说很好,但是应用程序在 Oracle 中不再工作(ORA-02289:序列不存在)。 I think Hibernate is then searching for a sequence called native in the Oracle DB. 我认为Hibernate 然后在 Oracle DB 中搜索名为 native 的序列。

Is there a solution to keep the ID generation the same for all the other DB engines (Oracle, PostgreSQL and SQL Server) and use the IDENTITY for MySQL?是否有解决方案可以使所有其他数据库引擎(Oracle、PostgreSQL 和 SQL Server)的 ID 生成保持相同并使用 MySQL 的 IDENTITY?

If not the only solution I can see is to implement the TABLE ID generation in MySQL, but it seems not to have a good performance .如果不是我能看到的唯一解决方案是在 MySQL 中实现 TABLE ID 生成,但它似乎没有很好的性能

I almost had it in the question, but in case somebody falls in the same situation: calling the generator HIBERNATE_SEQUENCE will make it backwards compatible with Oracle and PostgreSQL (SQL Server will continue using IDENTITY).我几乎在问题中得到了它,但如果有人遇到相同的情况:调用生成器 HIBERNATE_SEQUENCE 将使其向后兼容 Oracle 和 PostgreSQL(SQL Server 将继续使用 IDENTITY)。

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "HIBERNATE_SEQUENCE")
@GenericGenerator(name = "HIBERNATE_SEQUENCE", strategy = "native")
protected Long id;

Hey this question/answer got me on the road to the answer.嘿,这个问题/答案让我走上了寻找答案的道路。 I am using Hibernate core 5.4.30 with MSSQL SQLServer2012Dialect and Oracle Oracle12cDialect.我将 Hibernate core 5.4.30 与 MSSQL SQLServer2012Dialect 和 Oracle Oracle12cDialect 一起使用。 I needed to use the @GenericGenerator annotation as well for it's native strategy.我需要使用 @GenericGenerator 注释以及它的原生策略。 maybe I could have used this directly with @GeneratedValue?也许我可以直接与@GeneratedValue 一起使用它? anyway...反正...

I found Oracle did not come with a sequence named HIBERNATE_SEQUENCE, and even when I added one, hibernate was still giving me sequence not found errors, seemingly having to do with my schema... So you can make things more specific like this:我发现 Oracle 没有附带一个名为 HIBERNATE_SEQUENCE 的序列,即使我添加了一个序列,hibernate 仍然给我序列未找到的错误,似乎与我的模式有关......所以你可以像这样使事情更具体:

    @Id
    @GenericGenerator(name = "MY_SEQUENCE", strategy = "native",
        parameters = {
            @org.hibernate.annotations.Parameter(name = "schema", value="MY_SCHEMA"),
            @org.hibernate.annotations.Parameter(name = "sequence_name", value="MY_SEQUENCE")
        }
    )
    @GeneratedValue(generator = "MY_SEQUENCE")
    private Long id;

Hypothetically the name of the @GenericGenerator only has to match the generator name in the @GeneratedValue, but I made it the same as my actual database sequence for simplicity.假设@GenericGenerator 的名称只需与@GeneratedValue 中的生成器名称匹配,但为了简单起见,我将其与我的实际数据库序列相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM