"SELECT... WHERE column > ?1" - what is that?

Gibor :

I'm new to spring, fast-learned it for a few days and need to fix an old service in my workplace.

I've got a JPA repository, and some of the methods with @Query contains this weird > ?1 inside of them.

It looks like this:

@Repository
public interface LeadsRepository extends JpaRepository<LeadEntity, Long> 
{

    @Query(value = "select /*long query*/ where p.acceptedAt > ?1 and 
          p.acceptedAt < ?2", nativeQuery = true)
    Stream<LeadEntity> findPendingPackages(date begin, date end);
}

What does it mean? also how can it be that it selects both >1 and <2 ? it should select nothing then...

P.S - I 'm also a SQL newbie so go easy on me :)

Springer F :
@Query(value = "select /*long query*/ where p.acceptedAt > ?1 and 
          p.acceptedAt < ?2", nativeQuery = true)
Stream<LeadEntity> findPendingPackages(date begin, date end);

this mean that the ?1 refer to the first param of method => date begin

and ?2 referrs to the seconde param date end etc ...

So findPendingPackages should return all package that their acceptedAt date is between date begin and date end

so you can pass SQl parameters by using the ? , and you have to respect the number and the order of those in your method

see here in doc for further info

Also you can use Named Parameters like :paramname to prevent ordering error or number if you have a lot of param to send , by example your query would be

@Query(value = "select /*long query*/ where p.acceptedAt > :begin and 
          p.acceptedAt < :end", nativeQuery = true)
Stream<LeadEntity> findPendingPackages(@Param("begin") date begin,@Param("end") date end);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145065&siteId=1