Bom dia pessoal!
No meu BD Oracle, eu possuo "um" database, porém algumas tabelas foram criadas com o "usuario1" e outras tabelas com o "usuario2". Olhei no blog da Caelum e ví um post para acessar dois BD distintos, tentei adaptar para meu caso... Porém no persistence.xml gera um alerta:
Multiple persistence units defined - only the first persistence unit will be recognized
Alterei o persistence.xml para:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="usuario1">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/usuario1DS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@ipDoServidor:1521/meuDB" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.username" value="usuario1" />
<property name="hibernate.connection.password" value="senhaBD" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- OPCIONAIS -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
<persistence-unit name="ebfmes">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/usuario2DS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@ipDoServidor:1521/meuBD" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.username" value="usuario2" />
<property name="hibernate.connection.password" value="senhaBD" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- OPCIONAIS -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Criei a anotação para o usuario1:
package br.com.empresa.infra;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Qualifier
public @interface Usuario1 {
}
Criei a anotação para o usuario2:
package br.com.empresa.infra;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Qualifier
public @interface Usuario2 {
}
Alterei meu JPAUtil (EntityManagerProducer), para:
package br.com.empresa.infra;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ApplicationScoped
public class JPAUtil implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceUnit(unitName = "usuario1")
private EntityManagerFactory usuario1Factory;
@PersistenceUnit(unitName = "usuario2")
private EntityManagerFactory usuario2Factory;
// @Produces => Para informar ao CDI que esse será o método utilizado para criar as E.M.
// @RequestScoped => Para criar um E.M. por requisição WEB.
@Produces
@Usuario1
@RequestScoped
public EntityManager createUsuario1EntityManager() {
return usuario1Factory.createEntityManager();
}
@Produces
@Usuario2
@RequestScoped
public EntityManager createUsuario2EntityManager() {
return usuario2Factory.createEntityManager();
}
// @Disposes => Para informar ao CDI que esse será o método que fecha o E.M.
public void close(@Disposes EntityManager manager) {
if (manager.isOpen()) {
manager.close();
}
}
}
E adicionei a anotação no ProdutoDao a anotação do usuario1, pois essa tabela foi criada com esse usuario:
@Inject
public ProdutoDao(@Usuario1 EntityManager manager) {
this.manager = manager;
}