Searching for data in another bank schema using createQuery

0

In postgres, we can fetch data in multiple schemas through the same sql. Doing so:

SELECT * FROM meu_schema.minha_tabela

Is it possible to do this using createQuery ? By default, my application is using public schema, but I wanted to do a search on another schema in a particular method.

Something like:

Query query = entityManager.createQuery("SELECT s FROM schema2.Simulacao s");
    
asked by anonymous 18.11.2016 / 19:29

1 answer

1

You should create an EntityManagerFactory and EntityManager for each schema.

Ex.

javax.persistence.Persistence.createEntityManagerFactory esquema1 = javax.persistence.Persistence.createEntityManagerFactory("Esquema_1");
javax.persistence.Persistence.createEntityManagerFactory esquema2 = javax.persistence.Persistence.createEntityManagerFactory("Esquema_2");

EntityManager em1 = esquema1.createEntityManager();
EntityManager em2 = esquema2.createEntityManager();

Query query1 = em1.createQuery("SELECT s FROM Simulacao s");
Query query2 = em2.createQuery("SELECT s FROM Simulacao s");
    
18.11.2016 / 20:50