user3193861
user3193861

Reputation: 43

Using Variable in Spring @Query

I'm having an issue getting my variable to work in my @Query. Below is the code

@Query("SELECT new User(userId, userEmail, userForename, userSurname, userMiddleName) "
        + "FROM User "
        + "ORDER BY :orderBy DESC")
public List<User> findAllBy(@Param("orderBy") String orderBy);

I know the variable I want is being passed in properly. At the moment this is just returning all the results ordered by userId. If I hard code the value which is being passed into this function then it correctly returns the results ordered by userEmail (which is what is being passed in). Any suggestions would be great.

Upvotes: 0

Views: 138

Answers (1)

amseager
amseager

Reputation: 6391

Unfortunately, you can pass the parameters only to "conditional" clauses (like "where") due to underlying JDBC restrictions.

Instead of it consider to use overloaded PageRequest "of" method.

public static PageRequest of(int page,
                         int size,
                         Sort.Direction direction,
                         String... properties)

Upvotes: 1

Related Questions