Hi!! I have a mess with this classes and I would thank some help with them!!
This is my problem: I have an application with 2 Persistence Units ("UP_Produccion" and "UP_Practicas"), and I need to be connected to both all the time (because clients will need to execute querys on both).
I had it like this: (this is an example of class "UsuarioFacade"):
@PersistenceContext(unitName="UP_Produccion")
private EntityManager em_produccion;
@PersistenceContext(unitName="UP_Practicas")
private static EntityManager em_practicas;
public List validar(int entorno,
String consulta) {
if (entorno == 1){
return em_practicas.createQuery(consulta).getResultList();
}else{
return em_produccion.createQuery(consulta).getResultList();
}
}
It worked ok, but now, I�m trying to obtain EntityManager objetcs from another class (in order to avoid repeating this code in every Facade).
I would like to have something like this in UsuariosFacade:
public List validar(int entorno, String consulta) {
Controler.createQuery(entorno, consulta);
}
And Controler should choose the EntityManager to work with:
@PersistenceContext(unitName="UP_Produccion")
private static EntityManager em_produccion;
@PersistenceContext(unitName="UP_Practicas")
private static EntityManager em_practicas;
public List createQuery (int entorno, String consulta){
if (entorno == 1){
return em_practicas.createQuery(consulta).getResultList();
}else{
return em_produccion.createQuery(consulta).getResultList();
}
}
It didn�t work, so I tried to have EntityManagerFactory objects instead of EntityManager:
EntityManagerFactory emf_prod;
EntityManagerFactory emf_pract;
private ControlConexion() {
emf_prod = Persistence.createEntityManagerFactory("UP_Produccion");
emf_pract = Persistence.createEntityManagerFactory("UP_Practicas");
}
public List createQuery (int entorno, String consulta){
if (entorno == 1){
return emf_pract.createEntityManager().createQuery(consulta).getResultList();
}else{
return emf_prod.createEntityManager().createQuery(consulta).getResultList();
}
}
And it didn�t work either!!
I�m not sure if I�m using these objects propertly. Could anyone help me???
(I'm sorry if my English is not good. Please, ask me if I didn�t explain anything clearly...)
Thanks a lot for your help!!