简体   繁体   中英

Spring boot: Optional parameter query in Query method

I am new to Spring boot and hibernate. Here I am trying run a search based optional parameter query Where i can search by name, country etc. If I kept this field null then query should all list. But the problem is my method is returning all data ignoring my search parameter. my model class look like

@Entity(name="MLFM_ORDER_OWNER")
public class ModelOrderOwner {

    @Id @GenericGenerator(name = "custom_sequence", strategy = 
            "com.biziitech.mlfm.IdGenerator")
    @GeneratedValue(generator = "custom_sequence")
    @Column(name="ORDER_OWNER_ID")
    private Long orderOwnerId;

    @Column(name="OWNER_NAME")
    private String ownerName;

    @OneToOne
    @JoinColumn(name="BUSINESS_TYPE_ID")
    private ModelBusinessType businessTypeId;

    @Column(name="SHORT_CODE")
    private String shortCode;


    @ManyToOne
    @JoinColumn(name="OWNER_COUNTRY")
    private ModelCountry ownerCountry;
// getter setter..

My Repository interface looks like

public interface OrderOwnerRepository extends 

    JpaRepository<ModelOrderOwner,Long>{
        @Query("select a from MLFM_ORDER_OWNER a where a.businessTypeId.typeId=coalsec(:typeId,a.businessTypeId.typeId) and a.ownerCountry.countryId=coalsec(:countryId,a.ownerCountry.countryId) and a.ownerName LIKE %:name and a.shortCode LIKE %:code")
        public List <ModelOrderOwner> findOwnerDetails(@Param("typeId")Long typeId,@Param("countryId")Long countryId,@Param("name")String name,@Param("code")String code);

    }

And here is my method in controller

@RequestMapping(path="/owners/search")
     public String getAllOwner(Model model,@RequestParam("owner_name") String name,@RequestParam("shortCode") String code,

                            @RequestParam("phoneNumber") String phoneNumber,@RequestParam("countryName") Long countryId,
                            @RequestParam("businessType") Long typeId
             ) {
 model.addAttribute("ownerList",ownerRepository.findOwnerDetails(typeId, countryId, name, code));

            return "data_list";
    }

Can Any one help me in this regard? please?

It is too late too answer, but for anyone who looks for a solution yet there is a more simple way as below:

In my case my controller was like:

@RestController
@RequestMapping("/order")
public class OrderController {

    private final IOrderService service;

    public OrderController(IOrderService service) {
        this.service = service;
    }

    @RequestMapping(value = "/{username}/", method = RequestMethod.GET)
    public ResponseEntity<ListResponse<UserOrdersResponse>> getUserOrders(
            @RequestHeader Map<String, String> requestHeaders,
            @RequestParam(required=false) Long id,
            @RequestParam(required=false) Long flags,
            @RequestParam(required=true) Long offset,
            @RequestParam(required=true) Long length) {
        // Return successful response
        return new ResponseEntity<>(service.getUserOrders(requestDTO), HttpStatus.OK);
    }
}

As you can see, I have Username as @PathVariable and length and offset which are my required parameters, but I accept id and flags for filtering search result, so they are my optional parameters and are not necessary for calling the REST service.

Now in my repository layer I have just created my @Query as below:

@Query("select new com.ada.bourse.wealth.services.models.response.UserOrdersResponse(FIELDS ARE DELETED TO BECOME MORE READABLE)" +
        " from User u join Orders o on u.id = o.user.id where u.userName = :username" +
        " and (:orderId is null or o.id = :orderId) and (:flag is null or o.flags = :flag)")
Page<UserOrdersResponse> findUsersOrders(String username, Long orderId, Long flag, Pageable page);

And that's it, you can see that I checked my optional arguments with (:orderId is null or o.id =:orderId) and (:flag is null or o.flags =:flag) and I think it needs to be emphasized that I checked my argument with is null condition not my columns data , so if client send Id and flags parameters for me I will filter the Result with them otherwise I just query with username which was my @PathVariable .

Don't know how but below code is working for me

@Query("select a from MLFM_ORDER_OWNER a  where a.businessTypeId.typeId=COALESCE(:typeId,a.businessTypeId.typeId) and a.ownerCountry.countryId=COALESCE(:countryId,a.ownerCountry.countryId) and a.ownerName LIKE %:name and a.shortCode LIKE %:code")
    public List <ModelOrderOwner> findOwnerDetails(@Param("typeId")Long typeId,@Param("countryId")Long countryId,@Param("name")String name,@Param("code")String code); 

and in controller

@RequestMapping(path="/owners/search")
     public String getAllOwner(Model model,@RequestParam("owner_name") String name,@RequestParam("shortCode") String code,

                            @RequestParam("phoneNumber") String phoneNumber,@RequestParam("countryName") Long countryId,
                            @RequestParam(value = "active", required = false) String active, @RequestParam("businessType") Long typeId
             ) {




        if(typeId==0)
            typeId=null;
        if(countryId==0)
            countryId=null;

         model.addAttribute("ownerList",ownerRepository.findOwnerDetails(typeId, countryId, name, code, status));

            return "data_list";
    }

JPQL doesn't support optional parameters. There is no easy way of doing this in JPQL. You will have to write multiple WHERE clauses with OR operator.

Refer these answers to similar questions: Answer 1 & Answer 2

PS: You might want to look into Query by Example for your use case. It supports handling of null parameters.

Use JpaSpecificationExecutor // import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

Step 1 : Implement JpaSpecificationExecutor in your JPA Repository

Ex:

public interface TicketRepo extends JpaRepository<Ticket, Long>, JpaSpecificationExecutor<Ticket> {

Step 2 Now to fetch tickets based on optional parameters you can build Specification query using CriteriaBuilder

Ex:

public Specification<Ticket> getTicketQuery(Integer domainId, Calendar startDate, Calendar endDate, Integer gameId, Integer drawId) {
    return (root, query, criteriaBuilder) -> {
        List<Predicate> predicates = new ArrayList<>();

        predicates.add(criteriaBuilder.equal(root.get("domainId"), domainId));
        predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createdAt"), startDate));
        predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("createdAt"), endDate));

        if (gameId != null) {
            predicates.add(criteriaBuilder.equal(root.get("gameId"), gameId));
        }

        return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
    };
}

Step 3: Pass the Specification instance to jpaRepo.findAll(specification), it will return you the list of your entity object (Tickets here in the running example)

ticketRepo.findAll(specification); // Pass output of function in step 2 to findAll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM