Searching for data in another database Schema using createQuery

Asked

Viewed 57 times

0

In postgres, we can search for data in various schemas through sql itself. Doing so:

SELECT * FROM meu_schema.minha_tabela

It is possible to do this using the createQuery? By default, my application is using the public schema, but wanted to do a search in another schema in a given method.

Something like:

Query query = entityManager.createQuery("SELECT s FROM schema2.Simulacao s");

1 answer

1

You must create an Entitymanagerfactory and Entitymanager for each scheme.

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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.