Skip to content
Advertisement

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.

Advertisement

Answer

I think that you want CONCAT():

and name LIKE CONCAT('%', ?2, '%')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement