title: How to write native SQL queries in Hibernate without hardcoding table names and fields?
body: <p>Sometimes you have to write some of your queries in native SQL rather than hibernate HQL. Is there a nice way to avoid hardcoding table names and fields and get this data from existing mapping? </p>
  
  <p>For example instead of:</p>
  
  <pre><code>String sql = "select user_name from tbl_user where user_id = :id";
  </code></pre>
  
  <p>something like:</p>
  
  <pre><code>String sql = "select " + Hibernate.getFieldName("user.name") + " from " + Hibernate.getTableName(User.class) + " where " + Hibernate.getFieldName("user.id") + " = :id";
  </code></pre>