Hibernate 4从HT_tables中删除IN子句Token unknown“,”

时间:2014-04-20 09:40:29

标签: hibernate firebird jaybird

使用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)

Firebird例外:

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 + ")" );          
        return delete.toStatementString();
    }

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

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

1 个答案:

答案 0 :(得分:0)

首先,最近的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 generateDelete代码:

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可以支持