从hql到sql的错误翻译

时间:2014-02-12 13:20:27

标签: hibernate hql jpa-2.0 jpql

在我的(hql)查询中,我有以下where-clause

LOWER( f_umlautfree2( replace(user.lastName, ' ', '')||replace(user.firstName, ' ', '')  ) ) LIKE LOWER ('%'||( f_umlautfree2( replace(replace(:name, ',', ''), ' ', '') ) )||'%')

如果我打印出“真正的”sql查询,它会以这种方式“翻译”:

lower(idmanlight.f_umlautfree2((replace(user0_.lastName||' '||'')||replace(user0_.firstName||' '||'')))) like lower(('%'||idmanlight.f_umlautfree2(replace(replace('%Croci%'||','||'')||' '||''))||'%'))

导致错误,例如“无功能替换(文本)......”

为什么所有,(在函数的括号之间)都被||替换?

这是完整的hql查询:

select
    distinct user.dbId,
    user.lastName,
    user.firstName
from
    User user  LEFT join user.permissions
where
    LOWER( f_umlautfree2( replace(user.lastName, ' ', '')||replace(user.firstName, ' ', '')  ) ) LIKE LOWER ('%'||( f_umlautfree2( replace(replace(:name, ',', ''), ' ', '') ) )||'%')
and
    (user.employee IS NOT NULL OR user.lecturer IS NOT NULL OR user.ethRelated IS NOT NULL OR user.pdbGuest IS NOT NULL)
and
    user.valid = :valid  
order by
    user.lastName, user.firstName asc

这是完整的结果sql查询:

select
     distinct user0_.dbId as col_0_0_,
     user0_.lastName as col_1_0_,
     user0_.firstName as col_2_0_
from
     idmanlight.NETHZ_USER user0_
left outer join
     idmanlight.NETHZ_USER_PERMISSION permission1_
         on user0_.dbId=permission1_.NETHZ_USER_dbId
left outer join
     idmanlight.PERMISSION permission2_
         on permission1_.permissions_dbId=permission2_.dbId
where
     (
         lower(idmanlight.f_umlautfree2((replace(user0_.lastName||' '||'')||replace(user0_.firstName||' '||'')))) like lower(('%'||idmanlight.f_umlautfree2(replace(replace('%Croci%'||','||'')||' '||''))||'%'))
     )
     and (
         user0_.employee is not null
         or user0_.lecturer is not null
         or user0_.ethRelated is not null
         or user0_.pdbGuest is not null
     )
     and user0_.valid=?
 order by
     user0_.lastName,
     user0_.firstName asc

1 个答案:

答案 0 :(得分:2)

HQL执行not support replace功能。您应该使用Dialect.registerFunction()注册。

here是如何操作的示例)