Hibernate 4 Delete from HT_tables where IN clause Token unknown “,”

不羁岁月 提交于 2019-12-13 04:06:02

问题


Using Hibernate 4, the below generated query failed :

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 Exception :

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

In the org.hibernate.hql.spi.TableBasedDeleteHandlerImpl Class there is 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();
    }

So i want to adapt the code to Firebird, what is the nearest Firebird syntaxe to do same job?

i just migrate to hibernate 4, and it apeare that every delete from the temporary tables added in hibernate 4 (prefixed with HT_ ...) is based on this method so it looks as an important issue.


回答1:


First the nearest Firebird syntaxe is

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

Second the right hibernate generateDelete code :

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();
}

I hope that this should be generalised in hibernate, because this is not a standard code, or could be supported by Firebird



来源:https://stackoverflow.com/questions/23180669/hibernate-4-delete-from-ht-tables-where-in-clause-token-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!