title: How to configure default database schema for native queries in pure JPA?
body: <p>Our situation is as follows:</p>
  
  <ul>
  <li>We use (mustly pure) JPA for persistence in our enterprise application.</li>
  <li>Due to performance reasons, we use a number of fairly complicated native queries here an there.</li>
  <li>When accessing the database (Oracle 11g), we use a database user <code>APP_ACCESS</code> which is different from the "owner" of the tables (<code>APP_OWNER</code>). This is a hard requirement from the database administation.</li>
  <li>At the moment the schema name (<code>APP_OWNER</code>) is hardcoded into the native queries, a-la:</li>
  </ul>
  
  <p><code>"SELECT * FROM " + DatabaseSchemaConstants.SCHEMA_NAME + ".LOCATION"</code></p>
  
  <p>Needless to say, we are not particularly happy about this hardcoding as changes of the "owner" user name would mean changes in the code - which is definitely not good for maintenance.</p>
  
  <p>So here is my question:</p>
  
  <p><strong>How could we configure the default database schema for native queries in pure JPA?</strong></p>
  
  <p>This is what I've found so far:</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/2737420/how-to-set-up-default-schema-name-in-jpa-configuration">How to set up default schema name in JPA configuration?</a></p>
  </blockquote>
  
  <p>Specificaly <a href="https://stackoverflow.com/a/5805132/303810">this answer</a> which proposes a combination of a persistence unit and an <code>orm.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
                   version="1.0"&gt;
   &lt;persistence-unit-metadata&gt;
  
    &lt;persistence-unit-defaults&gt;
     &lt;schema&gt;myschema&lt;/schema&gt;
    &lt;/persistence-unit-defaults&gt;
   &lt;/persistence-unit-metadata&gt;
  &lt;/entity-mappings&gt;
  </code></pre>
  
  <p>But I think this does not apply to the native queries (we're testing it at the moment). Another issue here is that <code>orm.xml</code> isn't really for configuration. (But its is still better than hardcoding in the Java code).</p>
  
  <p>Another post with a similar answer:</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/3211138/jpa-eclipselink-how-to-change-default-schema">JPA - EclipseLink - How to change default schema</a></p>
  </blockquote>
  
  <p>A further question:</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/4832579/getting-hibernate-default-schema-name-programmatically-from-session-factory">getting hibernate default schema name programmatically from session factory?</a></p>
  </blockquote>
  
  <p>See <a href="https://stackoverflow.com/a/4851218/303810">this answer</a> which suggests using <code>{h-schema}</code>. Looks suitable, but it is Hibernate-specific. We'd like to stay with "pure JPA". Another problem is that I could not find any mentions of {h-schema} in Hibernate documentation, so it is not safe for us to rely on this feature.</p>
  
  <p><strong>Are there standard JPA way to set the default database schema for everything - JPA entites and queries <em>as well as native queries</em>?</strong></p>
  
  <p>Alternatively this could be solved if we'd be able to use some "parameters" or "settings" set somewhere in configuration in our native queries.</p>