cheshire
cheshire

Reputation: 1159

like clause in JPA native sql query

I have a query that returns a list of products:

@Query(value = "Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 order by name",
        nativeQuery = true)
    List<Product> findAll(long id);

How do I add a LIKE clause to this?

When I execute this statement in my DB a get a result:

Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 AND name LIKE '%someString%' AND content LIKE '%someString%' order by name

but how do I add LIKE to my JPA native query?

I have tried:

@Query(value = "Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 and name LIKE '%?%' order by name",
            nativeQuery = true)
        List<Product> findAll(long id, String name);

and

@Query(value = "Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 and name LIKE '%:name%' order by name",
            nativeQuery = true)
        List<Product> findAll(long id,@Param("name") String name);

But none of these work.

Upvotes: 2

Views: 9032

Answers (1)

GMB
GMB

Reputation: 222392

I think that you want CONCAT():

and name LIKE CONCAT('%', ?2, '%')

Upvotes: 13

Related Questions