Reputation: 3367
The result sql should be:
"SELECT * FROM items where id LIKE '%"+ key + "%' or name LIKE '%"+ key + "%'";
here key is variable.I need to do this sql in spring data jpa.
I try like below and code is not working
@Transactional
public List<Item> findItemNameOrId(String key) {
return itemRepository.findByItemNameOrIdContaining(key);
}
this is ItemRepository
public interface ItemRepository extends JpaRepository<Item, Integer> {
List<Item> findByItemNameOrIdContaining(String key);
}
Upvotes: 0
Views: 13278
Reputation: 1008
Try like this and it should run
public interface ItemRepository extends JpaRepository<Item, Integer> {
@Query("SELECT i FROM items i where i.id LIKE %:key% or i.name LIKE %:key%")
List<Item> findByItemNameOrIdContaining(@Param("key") String key);
}
Upvotes: 7