1

Is there a way to have Query parameters be column names like the following? I get null returns and don't know how to print the full query to console before it's run.

@Query(
    value = "SELECT ?1 FROM MATRIX WHERE ?1 = ?2",
    nativeQuery = true)
List<String> findByExactFieldMatch_(String field, String value);
patrickjp93
  • 399
  • 4
  • 20
  • 1
    i don't think you can do that, you should provide the column name – Ryuzaki L Mar 04 '19 at 01:14
  • But that's ridiculous. Getting a single arbitrary column from a table should have a generic implementation. Writing 5+ queries to cover each one is a bit ridiculous. – patrickjp93 Mar 04 '19 at 01:34
  • 1
    https://stackoverflow.com/questions/47407043/spring-data-jpa-pass-column-name-and-value-as-parameters?rq=1 – Ryuzaki L Mar 04 '19 at 01:40
  • @Deadpool solution using specifications works to make dynamic filters at where. Nice catch! – Cristian Colorado Mar 04 '19 at 02:40
  • Hmm, I should raise a use case with the JPA team. It's utterly stupid that we don't have a generic getColumn(value) solution with native queries. This solution is clunky and has to be done for every single repository.. – patrickjp93 Mar 04 '19 at 02:58
  • This is not so much about Spring Data JPA, but about SQL and JDBC which allows bind variables only for values which is the same thing most programming languages do. If you want to specify column names dynamically you still have specifications (kind of like you have reflection for that in general programming languages). – Jens Schauder Mar 04 '19 at 06:05

0 Answers0