0
我想按任何順序通過名字,姓氏或兩者在我的用戶實體中搜索用戶。 例如: 在數據庫中我有這個用戶名:'jack'和姓''franck' ,我想輸入'ck'或'jack franck'或'franck jack'並返回此用戶。以任意順序搜索用戶的名字和/或姓氏
在JPQL(春季數據),我執行此查詢,但我沒有任何結果......
@Query("select u from User u where " +
"((CONCAT(u.firstName, ' ', u.lastName) like CONCAT('%', :user, '%') or :user is null) " +
"or (CONCAT(u.lastName, ' ', u.firstName) like CONCAT('%', :user, '%') or :user is null) " +
"or (u.firstName like CONCAT('%', :user, '%') or :user is null) " +
"or (u.lastName like CONCAT('%', :user, '%') or :user is null)) ")
編輯:
其實,我有一個需求的實體。在該實體,我有兩個用戶,參考等
整個請求是:
@Query("select d from Demande d where " +
"(d.reference like :reference or :reference is null) " +
"and (d.dateSouhaitee = :dateSouhaitee or :dateSouhaitee is null) " +
"and (d.datePricing = :datePricing or :datePricing is null) " +
"and (CONCAT(d.pricer.firstName, ' ', d.pricer.lastName) like :pricer or :pricer is null) " +
"and (CONCAT(d.commercial.firstName, ' ', d.commercial.lastName) like :commercial or :commercial is null) " +
"and (d.etat like :etat or :etat is null) " +
"and (d.client.raisonSociale like :clientRS or :clientRS is null) "
)
「定價者」和「商業」是用戶。
我的方法調用:
List<Demande> demandes = demandeRepository.search("%".concat("ref1").concat("%"), dateSouhaitee, datePricing, "%".concat("pricer1").concat("%"), null, null, "%".concat("du nord").concat("%"));
和生成的SQL是:
select
demande0_.id as id1_3_,
demande0_.client_id as client_i9_3_,
demande0_.commentaire_commercial as commenta2_3_,
demande0_.commentaire_pricer as commenta3_3_,
demande0_.commercial_id as commerc10_3_,
demande0_.contrat_id as contrat11_3_,
demande0_.date_pricing as date_pri4_3_,
demande0_.date_souhaitee as date_sou5_3_,
demande0_.etat as etat6_3_,
demande0_.pricer_id as pricer_12_3_,
demande0_.reference as referenc7_3_,
demande0_.sites as sites8_3_
from
demande demande0_ cross
join jhi_user user1_ cross
join jhi_user user3_ cross
join client client5_
where
demande0_.pricer_id = user1_.id
and demande0_.commercial_id = user3_.id
and demande0_.client_id = client5_.id
and (
demande0_.reference like ?
or ? is null
)
and (
demande0_.date_souhaitee = ?
or ? is null
)
and (
demande0_.date_pricing = ?
or ? is null
)
and (
(
user1_.first_name || ' ' || user1_.last_name
) like ?
or ? is null
)
and (
(
user3_.first_name || ' ' || user3_.last_name
) like ?
or ? is null
)
and (
demande0_.etat like ?
or ? is null
)
and (
client5_.raison_sociale like ?
or ? is null
)
感謝:-)
並且調用的SQL是? –
@BillyFrost - 我編輯了我的帖子。謝謝。 – slim
那麼,請求的demande是否有一個非空的商業廣告,一個非null的定價器和一個非空的客戶?因爲你正在對這三個關聯進行內部連接。您處理空參數的方式不正確。根據參數動態創建查詢。這就是API的標準。 –