title: JPA native queries: hack for column and table names as variables at runtime
body: <p>I understand it's not possible to dynamically modify table names and column names in a JPA query. They have to be hard-coded. </p>
  
  <p>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:</p>
  
  <pre><code>public interface ExampleRepository extends JpaRepository&lt;Example, Long&gt;,
      JpaSpecificationExecutor&lt;Example&gt; {
  
      @Query(value = "SELECT name_ln FROM ?1 WHERE ?2 = ?3",
          nativeQuery = true)
      String getName(String tableLookUp, String idColumn, long namespaceRefId);
  }
  </code></pre>
  
  <p>This doesn't work, with MySQL producing the following error due to quotes around the table and column names:</p>
  
  <pre><code>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
  </code></pre>