Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how can I set schema in run time ? I want run a name query in more than one database and aggregate their answers.

for example run this query in db1 and db2 : SELECT CUSTOMER FROM CUSTOMER CUS WHERE CUS.CODE=?1

note : I can set schema on @Table in entities ,but it isn't dynamic and i can't change it in run time.

please help! regards

share|improve this question

1 Answer

One solution is to setup N datasources each pointing to different database/schema:

DataSource ds1 = // setup ds 1
DataSource ds2 = // setup ds 2

and N entity manager factories (EMF) with 1-to-1 mapping with each ds :

EntityManagerFactory emf1 = // setup emf1 mapped to ds1
EntityManagerFactory emf2 = // setup emf2 mapped to ds2

Then you need N copies of each DAO classes injected with each EMF :

public class CustomerDAO {

  private EntityManagerFactory emf;

  public CustomerDAO(EntityManagerFactory emf) {
    this.emf = emf;
  }

  // dao methods here..
}

CustomerDAO emf1CustomerDAO = // setup customer DAO injected with emf1
CustomerDAO emf2CustomerDAO = // setup customer DAO injected with emf2

Your application can now make a decision at runtime which database/schema to use by selecting the DAO

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.