I understand it's not possible to dynamically modify table names and column names in a JPA query. They have to be hard-coded.
However, I have a schema that has lots of different tables all with the same structure for the first three columns. Rather than having to write a class or a method for querying each of these I wonder if there's a quick hack for modifying the table name and column names using variables? My existing query looks like:
public interface ExampleRepository extends JpaRepository<Example, Long>, JpaSpecificationExecutor<Example> { @Query(value = "SELECT name_ln FROM ?1 WHERE ?2 = ?3", nativeQuery = true) String getName(String tableLookUp, String idColumn, long namespaceRefId); } This doesn't work, with MySQL producing the following error due to quotes around the table and column names:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''table_name' WHERE 'column_id' = 211833' at line 1