Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in Spring Data JPA native query

Using Spring Dat JPA, I need to query my database and return a range of OrderEntitys based on a startAmt and a endAmt of amounts. I'm not sure if I should map these two variables to entity OrderEntity, as fields in some type of separate class/entity/model, or simply declare them in my native query. Perhaps I should be using a service that implements EntityManager.createNativeQuery()?

Would like to do something like :


@Repository
public interface OrderRangeRepository extends JpaRepository<OrderEntity, OrderEntityID> {

        @Query(value = "SELECT * FROM Orders WHERE Amount BETWEEN startAmt AND endAmt;" , nativeQuery=true)
    List<OrderEntity> findOrdersBy(int startAmt, int endAmt);

}

If I were to use EntityManager.createNativeQuery() in a service, perhaps something like below :

@Service
public class OrderRangeService {

    @Autowired
    EntityManager entityManager;

    public List<OrderEntity> findAmountsBetween() {

        List<OrderEntity> amountsBetween = entityManager.createNativeQuery("SELECT * FROM Orders WHERE Amount BETWEEN ?1 AND 2?;")
        .setParameter(1, "startAmt")
        .setParameter(2, "endAmt")
        .getResultList();

        return amountsBetween;


    }

}

like image 792
wallwalker Avatar asked Mar 07 '26 14:03

wallwalker


1 Answers

You can achieve this with Spring Data JPA without defining a native query.

@Repository
public interface OrderRangeRepository extends JpaRepository<OrderEntity, OrderEntityID> {
    List<OrderEntity> findByAmountBetween(int startAmt, int endAmt);
}

If you want to use the native query change it to

 @Query(value = "SELECT * FROM Orders WHERE Amount BETWEEN :startAmt AND :endAmt" , nativeQuery=true)
List<OrderEntity> findOrdersBy(@Param("startAmt") int startAmt, @Param("endAmt") int endAmt);

You can invoke the query in a service by doing

@Service
public class OrderRangeService {

    @Autowired
    OrderRangeRepository orderRangeRepository ;

    public List<OrderEntity> findAmountsBetween(int startAmt, int endAmt) {
        List<OrderEntity> amountsBetween = orderRangeRepository.findByAmountBetween(startAmt, endAmt);
        return amountsBetween;
    }

}

Finally, from your controller, you should autowire the OrderRangeService and invoke the findAmountsBetween service method

@Autowired
OrderRangeService orderRangeService;

@GetMapping("/amountsFromAndTo")
@ResponseBody
public String getAmounts(@RequestParam int startAmt, @RequestParam int endAmt) {
    List<OrderEntity> orderEntityL = orderRangeService.findAmountsBetween(startAmt, endAmt);
    return orderEntityL.toString();
}
like image 85
Ioannis Barakos Avatar answered Mar 10 '26 04:03

Ioannis Barakos