Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 970
Aritra Paul Avatar asked Oct 28 '25 17:10

Aritra Paul


1 Answers

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.

like image 154
Sobhan Avatar answered Oct 31 '25 06:10

Sobhan