gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:33:45 25 4
gpt4 key购买 nike

有没有一种方法可以管理或更改 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com