Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA (Hibernate) + Tomcat + two different databases
    primarykey
    data
    text
    <p>I have a webapp using JPA provided by Hibernate and Tomcat. I have no DI framework, no Spring, no Guice, no full Java EE app server.</p> <p>I need JPA to connect to two different databases. I connect to the first database like this:</p> <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb"); [...] EntityManager em = emf.createEntityManager(); </code></pre> <p>and I have the "mydb" persistence unit declared in persistence.xml and linked through JNDI to its resource declared in context.xml. Till here everithing works.</p> <p>Now I need to connect to another database. I've added it to persistence.xml and context.xml and named the persistence unit "mydb2". Now I assume I can use it like this:</p> <pre><code>EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("mydb2"); EntityManager em2 = emf2.createEntityManager(); </code></pre> <p>Hibernate is configured to validate classes and tables on both databases with </p> <pre><code>&lt;property name="hibernate.archive.autodetection" value="class, hbm"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt; </code></pre> <p>in persistence.xml. However when I run/debug the webapp, Hibernate tries to validate @Entity classes that belong to mydb2 on mydb, and it obviously fails. Now the fun part is that if I set the Hibernate validation property to "none", my application does work correctly and connects to the correct db as needed...</p> <p>Is there a way to have Hibernate validate only the classes/tables belonging to their respective database?</p> <p>EDIT: While it's possible to specify a catalog name in @Table annotations, in my case it's not a good solution, because I have more than one instance of this webapp in production and each instance connects to a different pair of databases. Being forced to touch the source code just before going into production is bad.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload