spring-bootspring-data-jpajpqlnativequeryhibernate-native-query

SpringDataJPA - What does '?1' in the where clause mean in the below query?


@Query(value="select * from author a where a.last_name= ?1",nativeQuery = true)

What does ?1 mean in the above query?


Solution

  • This ?1 indicate a positional parameters is a different practice for define a query parameters to advoid some issue like SQL injection, for example:

        String statment = "SELECT * from Users WHERE name=:nameParam and role=:roleParam";
    
        //some code
    
        query.setString("nameParam", nameValue);
    
        query.setString("roleParam", roleValue);
    
        String statment = "SELECT * from Users WHERE name=?1 and role=?2";
        
        //some code
    
        query.setString(1, nameValue);
    
        query.setString(2, roleValue);