user5833031
user5833031

Reputation:

Spring boot jpa interface - make custom query

I have such a Interface:

interface ItemRepository extends JpaRepository<Item, Integer> {
    Item findItemByName(String name);
    Collection<Item> findItemByCategory(String category);
}

it does the job without implementation already, but I have to add the following statement:

select from Item where quantity < 10;

Upvotes: 2

Views: 395

Answers (2)

Wael Sakhri
Wael Sakhri

Reputation: 397

You can use LessThan identifier or use a custom query like this :

@Query("SELECT i FROM Item i WHERE i.quantity < :quantity")
public List<Item> findByCategory(@Param("quantity") int quantity);

Upvotes: 0

Tunaki
Tunaki

Reputation: 137329

Spring Data JPA supports the LessThan keyword inside method names. In your case, the signature of the method would be:

Collection<Item> findItemByQuantityLessThan(int upperBound);

You can then call this method giving it 10 as parameter to have your result.

Upvotes: 2

Related Questions