Hibernate没有为(hbm文件)映射文件中的Hibernate Named Query生成正确的sql查询

时间:2016-01-13 13:40:38

标签: hibernate orm hql hibernate-mapping hibernate-criteria

我在hbm文件中定义了一个名为hibernate的命名查询。

<query name="AddressBook.find_by_user_id_and_type_lease_time">
        <![CDATA[ FROM AddressBook addressBook 
        WHERE addressBook.mightyUser.userId = ? 
        and addressBook.typeId = ? 
        and (addressBook.lockOwner is null
        or (addressBook.lockOwner is not null 
        and (sysdate - addressBook.lockLeaseDate) > ?))]]>
    </query> 

通过hibernate Query接口执行此查询时,发现生成的查询不正确。这里'('被忽略/从查询中删除。

 2016-01-13 12:26:44.327 475eb4d0 D 00000000000000000000000000000000    02a5:select addressboo0_.ADDRESS_BOOK_ID as ADDRESS1_37_, addressboo0_.VERSION as VERSION37_, addressboo0_.ADDRESS_BOOK_NAME as ADDRESS3_37_, addressboo0_.UPDATED_TIME as UPDATED4_37_, addressboo0_.CREATED_DT as CREATED5_37_, addressboo0_.TYPE_ID as TYPE6_37_, addressboo0_.LOCK_OWNER as LOCK7_37_, addressboo0_.DEDUPE_DATE as DEDUPE8_37_, addressboo0_.LOCK_LEASE_DATE as LOCK9_37_, addressboo0_.ACCOUNT_NAME as ACCOUNT10_37_, addressboo0_.USER_ID as USER11_37_ from R4GDEV02_MBW.ADDRESSBOOK addressboo0_ where addressboo0_.USER_ID=? and addressboo0_.TYPE_ID=? and (addressboo0_.LOCK_OWNER is null or 
***(addressboo0_.LOCK_OWNER is not null) and sysdate-addressboo0_.LOCK_LEASE_DATE>?)***

请建议/建议

1 个答案:

答案 0 :(得分:1)

SQL查询是正确的,因为and的优先级高于or。所以你不需要括号 (addressBook.lockOwner is not null and (sysdate - addressBook.lockLeaseDate) > ?)

更新

(addressBook.lockOwner is null or 
    (addressBook.lockOwner is not null and (sysdate - addressBook.lockLeaseDate) > ?))

装置

(A + (B * C))

(addressBook.lockOwner is null or 
    (addressBook.lockOwner is not null) and sysdate - addressBook.lockLeaseDate > ?)

表示

(A + B * C)

它是一样的

(A + (B * C)) = (A + B * C)