0

使用 Hibernate 4,以下生成的查询失败:

delete from ErpEmploye_AUD where (code, folder_codeId, REV) 
    IN 
(select code, folder_codeId, REV from HT_ErpEmploye_AUD where hib_sess_id=1)

火鸟例外:

SQL error code = -104
Token unknown - line 1, column 39
,

org.hibernate.hql.spi.TableBasedDeleteHandlerImpl课堂上有 https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/hql/spi/TableBasedDeleteHandlerImpl.java

private String generateDelete(String tableName, String[] columnNames, 
                String idSubselect, String comment) {
    final Delete delete = new Delete()
        .setTableName( tableName )
        .setWhere( "(" + StringHelper.join( ", ", columnNames ) +

") IN (" + idSubselect + ")" );
返回 delete.toStatementString(); }

所以我想使代码适应 Firebird,做同样工作的最接近的 Firebird 语法是什么?

我刚刚迁移到 Hibernate 4,似乎 Hibernate 4 中添加的临时表(以 HT_ ... 为前缀)的每次删除都是基于此方法,因此它看起来是一个重要问题。

4

1 回答 1

1

首先最接近的 Firebird 语法是

delete from ErpEmploye_AUD e where 
exists( select code, folder_codeId, REV from HT_ErpEmploye_AUD ht
 where ( hib_sess_id=1 and e.code=ht.code and e.folder_codeId=ht.folder_codeId and e.REV=ht.REV ))

二对Hibernate生成删除代码:

private String generateDelete(String tableName, String[] columnNames, String idSubselect, String comment) {
    String[] columnEquals = new String[columnNames.length];
    for (int i=0;i<columnNames.length;i++){
        columnEquals[i] = "tr."+columnNames[i]+"=ht."+columnNames[i];
    }
    if (idSubselect.contains("where"))
        idSubselect = idSubselect.replace("where", "ht where");
    else
        idSubselect = idSubselect + " ht where";
    final Delete delete = new Delete()
            .setTableName( tableName+" tr " )
            //.setWhere( "(" + StringHelper.join( ", ", columnNames ) + ") IN (" + idSubselect + ")" );
            .setWhere( "exists("+ idSubselect +" "+ StringHelper.join( " and ", columnEquals ) + ")" );     
    
    if ( factory().getSettings().isCommentsEnabled() ) {
        delete.setComment( comment );
    }
    return delete.toStatementString();
}

我希望这应该在 Hibernate 中推广,因为这不是标准代码,或者可以被 Firebird 支持

于 2014-04-20T11:29:42.897 回答