有没有办法可以管理或更改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.DB2400Dialect
任何人都有一些意见或建议如何解决这个问题?
答案 0 :(得分:2)
解决方案是使用:
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";
}
...