按任何顺序搜索用户的名字和/或姓氏

时间:2015-12-23 14:38:46

标签: jpa spring-data spring-data-jpa jpql

我希望以任意顺序通过firstName,lastname或两者搜索我的用户实体中的用户。 例: 在数据库中我有这个用户:firstname' 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
)

谢谢: - )

1 个答案:

答案 0 :(得分:0)

最后我使用了Criteria API。

@PersistenceContext
private EntityManager em;

@Override
public List<Demande> search(String reference, ZonedDateTime dateSouhaitee, ZonedDateTime datePricing, String pricer, String commercial, EnumEtatDemande etat, String clientRS) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Demande> query = builder.createQuery(Demande.class);
    Root<Demande> root = query.from(Demande.class);
    EntityType<Demande> model = root.getModel();

    List<Predicate> predicates = new ArrayList<>();
    if(reference != null){
        predicates.add(builder.equal(root.get(model.getSingularAttribute("reference")), reference));
    }
    if(dateSouhaitee != null){
        predicates.add(builder.equal(root.get(model.getSingularAttribute("dateSouhaitee")), dateSouhaitee));
    }
    if(datePricing != null){
        predicates.add(builder.equal(root.get(model.getSingularAttribute("datePricing")), datePricing));
    }
    if(pricer != null){
        Expression<String> pricerExpression = builder.concat(
            builder.concat(root.<String>get("pricer.firstName"), " "), root.<String>get("pricer.lastName"));
        predicates.add(builder.like(pricerExpression, pricer));
    }
    if(commercial != null){
        Expression<String> commercialExpression = builder.concat(
            builder.concat(root.<String>get("commercial.firstName"), " "), root.<String>get("commercial.lastName"));
        predicates.add(builder.like(commercialExpression, pricer));
    }
    if(etat != null){
        predicates.add(builder.equal(root.get(model.getSingularAttribute("etat")), etat));
    }
    if(clientRS != null){
        predicates.add(builder.equal(root.get(model.getSingularAttribute("client.raisonSociale")), clientRS));
    }
    query.select(root).where(predicates.toArray(new Predicate[predicates.size()]));
    return em.createQuery(query).getResultList();
}