Spring JPA filter optional criteria in Query method

mslowiak :

I want to filter out data from a database with few criteria (let's assume it is 8). And below query method do this in a good way. But in fact, this criterias passed to the query method can be null (it means that should not be included to select query).

How I should handle this situation? I really don't want to make n-methods to handle each case - it is not a good way.

@Query("SELECT NEW api.model.GeneralAnnouncementInfo(" +
        "an.id, an.title, po.price, SUBSTRING(an.description, 1, 100), an.provider, an.creationDate, an.url, l.lessorType, concat(loc.city, ' ', loc.district)) " +
        "FROM Announcement as an " +
        "LEFT JOIN an.priceOffer as po " +
        "LEFT JOIN an.lessor as l " +
        "LEFT JOIN an.location as loc " +
        "LEFT JOIN an.propertyData as pd " +
        "WHERE l.lessorType = (:lessor) " +
        "AND pd.roomNumber = (:rooms) " +
        "AND pd.bathroomNumber = (:baths) " +
        "AND pd.parkingAvailability = (:parking) " +
        "AND pd.isSmokingAllowed = (:smokers) " +
        "AND pd.isPetFriendly = (:pets) " +
        "AND pd.area = (:realPrice) " +
        "AND po.price = (:area) ")
Page<GeneralAnnouncementInfo> getAnnouncementsBySearchCriteria(Pageable pageable, 
                                                               String lessor,
                                                               String rooms,
                                                               String baths,
                                                               String parking,
                                                               String smokers,
                                                               String pets,
                                                               String realPrice,
                                                               String area
);
Marco R. :

I would recommend switching to the JPA Criteria API. It will give you the extra flexibility you are seeking (and which JPQL seems to be maxing out for your case). You can build your queries programmatically without any limitations and the best thing is that they get compiled; which means that no typos will survive (which are a nightmare to track in JPQL queries). Additionally you may want to use JPA metamodel classes; which add more robustness to your queries. At the end, your repository method would look something like this:

private EntityManager em;

private Page<GeneralAnnouncementInfo> getAnnouncementsBySearchCriteria(QueryParameters qParams) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SampleEntity> criteria = cb.createQuery(GeneralAnnouncementInfo.class);
    Root<GeneralAnnouncementInfo> root = criteria.from(GeneralAnnouncementInfo.class);

    // Programmatically build query details (conditions, joins, aggregations, translation, etc) 
    // ...
    // ...
    // ...


    return em.createQuery(criteria).getResultList();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=96710&siteId=1