java - Hibernate 4 和 5 的 db2 中序列 SQL 的差异

标签 java sql hibernate jpa db2

有没有一种方法可以管理或更改 Hibernate 5 生成的 sql,以了解如何从 AS400 DB2 检索序列?或者恢复它以使用 Hibernate 4 SQL?

我有一个实体映射如下:

@Id
@Column(name = "D")
@SequenceGenerator(
        name = "art_id_gen",
        sequenceName = "G_ID_SEQ",
        allocationSize = 1)
@GeneratedValue(generator = "art_id_gen", strategy = GenerationType.SEQUENCE)
private long id;

当我使用 Hibernate 4 时,SQL 是(有效):

values nextval for G_ID_SEQ;

但是当使用 Hibernate 5 时,SQL 是(不起作用):

select next_val as id_val from G_ID_SEQ for update with rs;

我正在使用 org.hibernate.dialect.DB2400方言

有人对如何解决这个问题有一些意见或建议吗?

最佳答案

解决方案是使用:

org.hibernate.dialect.DB2Dialect

生成与 hibernate 4 相同的 sql:

values
    nextval for G_ID_SEQ

一年后,我在我的代码中偶然发现了这个问题,最终我们修补了 DB2400Dialect:

/**
 * This is a patched version of the PatchedDB2400Dialect which is working in hibernate 5 and DB2 AS400
 *
 * There seems to be a problem with generated SQL for sequences and tables generated SQL for increment of unique id's
 * and identities. Either the sequence work when using DB2Dialect and not table generators or vice versa for DB2400Dialect.
**/
public class PatchedDB2400Dialect extends DB2400Dialect {
        private static final LimitHandler LIMIT_HANDLER = new AbstractLimitHandler() {
            @Override
            public String processSql(String sql, RowSelection selection) {
                if ( LimitHelper.hasFirstRow( selection ) ) {
                    //nest the main query in an outer select
                    return "select * from ( select inner2_.*, rownumber() over(order by order of inner2_) as rownumber_ from ( "
                            + sql + " fetch first " + getMaxOrLimit( selection ) + " rows only ) as inner2_ ) as inner1_ where rownumber_ > "
                            + selection.getFirstRow() + " order by rownumber_";
                }
                return sql + " fetch first " + getMaxOrLimit( selection ) + " rows only";
            }
...

关于java - Hibernate 4 和 5 的 db2 中序列 SQL 的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267901/

相关文章:

java - 使用流从字符串中删除多个字符串

java - 如何在 Kotlin Gradle 脚本中导入 java 模块

java - JavaFX WebEngine 中的 Google map 热图错误

java - GreenRobot的eventbus看不到注解, "no public methods"

java - hibernate referencedColumnNames 未映射到单个属性

MYSQL|状态的数据类型

mysql - 查询整个mysql数据库

mysql - 选择其中 1 个属性恰好两次不同的所有行。 (MySQL)

java - 如何在没有完全限定名称的情况下在另一个生成的类中定义 java codeModel 生成的类

java - Hibernate 是否需要实体中的列名才能工作?