Query with dynamic spring data column

Asked

Viewed 2,382 times

3

I need to perform a select using the spring data @Query, but I need to pass the column name by parameter

Example:

@Query("SELECT g FROM Grupo g where g.? = ?") 
Page<Grupo> findTeste(String campo, String valor);

To call method I wanted to pass the column name as following:

//Pseudo code
page = grupoService.findTeste("id", "1");
page = grupoService.findTeste("nome", "asdf");

It is possible to build some method like this in spring data?

  • 1

    This way there is no way - not without changing the core of the framework -, the lookup of queries in spring data is in startup. What you can have is a custom repository, use Specifications, build queries dynamically, use something like querydsl, etc. If it is not mandatory to use @Query Then I’ll give you an answer.

  • It is not required to use @Query, I will study Specifications and the querydsl if you can send a code I appreciate.

2 answers

1

I believe that in this case there is no way to do this using Spring Data.

But you can create a class for example: Customrepository

In this class you can use the Entitymanager and run the query as you want, example:

@PersistenceContext
protected EntityManager manager;

public Grupo findTeste(String campo, String valor) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT g FROM Grupo g where g.").append(campo).append(" = :").append(valor);
    String query = sb.toString();
    return manager.createQuery(query, Grupo.class).getSingleResult();
}
  • Hehe, that was the answer that no one wanted to believe was the only solution. Very strange not to have this option in Spring Data. Spring brings so many advantages and this way there is no way to take advantage of them, as take advantage of Pageable for example and have to stay doing limit and offset in the hand.

1

In the documentation itself you have @Query example with Namedparameters:

--

5.3.6. Using named Parameters

By default Spring Data JPA will use position based Parameter Binding as described in all the samples above. This makes query methods a little error prone to Refactoring regarding the Parameter position. To Solve this Issue you can use @Param Annotation to Give a method Parameter a Concrete name and bind the name in the query. Example 52. Using named Parameters

public interface UserRepository extends JpaRepository<User, Long> {

  @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
  User findByLastnameOrFirstname(@Param("lastname") String lastname,
                                 @Param("firstname") String firstname);
}

Note that the method Parameters are Switched According to the Occurrence in the query defined.

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.named-Parameters

  • I don’t think you understand, I wanted to pass the name of the column, for example: page = grupoService.findTeste("id", "1");&#xA;page = grupoService.findTeste("nome", "asdf");

  • He didn’t really understand, his response applied to the column nor compiles... spring checks if the query is valid to compile..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.