Home > database >  Spring data JDBC custom query
Spring data JDBC custom query

Time:10-05

I have defined the following custom database query.

@Query("select po.* from purchase_order po where po.purchase_order_number = :purchaseOrderNumber")
List<PurchaseOrder> findByPurchaseOrderNumber(String purchaseOrderNumber);

This query always return null regardless of the value of purchas. On the other hand, if I replace the dynamic parameter purchaseOrderNumber in the query with a hard-coded value (as the one depicted below) it perfectly works.

@Query("select po.* from purchase_order po where po.purchase_order_number = "10")
List<PurchaseOrder> findByPurchaseOrderNumber(String purchaseOrderNumber);

I would appreciate it if someone can help me to understand why my query with the dynamic PurchaseOrderNumber doesn't work?

CodePudding user response:

Specify the name of the query parameter

@Query("select po.* from purchase_order po where po.purchase_order_number = :purchaseOrderNumber")
List<PurchaseOrder> findByPurchaseOrderNumber(@Param("purchaseOrderNumber") String purchaseOrderNumber);

CodePudding user response:

Try this:

@Query("select po.* from purchase_order po where po.purchase_order_number like ?1, nativeQuery = true)
List<PurchaseOrder> findByPurchaseOrderNumber(String purchaseOrderNumber);

I think for string data type it is better to use "like" than "="

  • Related