I'm trying to insert a second base in persistence.xml. I have already configured the datasource in jboss and tested it. When inserting a second persistence-unit in xml and attempting to raise jboss the error below occurs. I tried to set the @PersistenceContext (unitName="unit-name") and @PersistenceUnit classes in each DAO class, but to no avail. I tried to follow caelum's step by step ( this ), but I did not understand how to fit step by step in my context, nor do I know if this is the case.
I want to know how to configure for my case, I think the biggest difference is that I have an EntityManager coming from a GenericDAO and this is making it difficult for me to understand. If I have to annotate something else in DAOs or entities, should I have to create another abstract class?
17:37:11,411 INFO [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
17:37:11,491 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Limite da fonte de dados [java:jboss/datasources/ExampleDS]
17:37:11,492 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Limite da fonte de dados [java:jboss/datasources/intraDS]
17:37:11,491 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) WFLYJCA0001: Limite da fonte de dados [java:jboss/datasources/intrasis]
17:37:11,524 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) WFLYDS0013: Foi iniciado o FileSystemDeploymentService para o diretório C:\Users\vinicius.cipolli\Devlopment\jboss-eap-7.0\standalone\deployments
17:37:11,528 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Iniciando a implantação do "intranet.war" (runtime-name: "intranet.war")
17:37:11,621 INFO [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-8) ISPN000128: Infinispan version: Infinispan 'Mahou' 8.1.2.Final-redhat-1
17:37:11,661 INFO [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBossWS 5.1.3.SP1-redhat-1 (Apache CXF 3.1.4.redhat-1)
17:37:12,706 INFO [org.jboss.as.jpa] (MSC service thread 1-5) WFLYJPA0002: Leia a persistence.xml para intranet
17:37:12,707 INFO [org.jboss.as.jpa] (MSC service thread 1-5) WFLYJPA0002: Leia a persistence.xml para intrasis
17:37:12,708 WARN [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0065: Assinatura inválida para o init do método anotado com o javax.annotation.PostConstruct no br.sp.corpedu.intranet.domain.acod.service.EspecialidadeService da classe. A assinatura deve ser 'void methodName(InvocationContext ctx)'
17:37:12,709 WARN [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0065: Assinatura inválida para o init do método anotado com o javax.annotation.PostConstruct no br.sp.corpedu.intranet.expose.resources.CursoResource da classe. A assinatura deve ser 'void methodName(InvocationContext ctx)'
17:37:12,709 WARN [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0065: Assinatura inválida para o init do método anotado com o javax.annotation.PostConstruct no br.sp.corpedu.intranet.expose.resources.EspecialidadeResource da classe. A assinatura deve ser 'void methodName(InvocationContext ctx)'
17:37:12,709 WARN [org.jboss.as.ee] (MSC service thread 1-5) WFLYEE0065: Assinatura inválida para o init do método anotado com o javax.annotation.PostConstruct no br.sp.corpedu.intranet.expose.resources.estoque.ItemResource da classe. A assinatura deve ser 'void methodName(InvocationContext ctx)'
17:37:12,719 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."intranet.war".DEPENDENCIES: org.jboss.msc.service.StartException in service jboss.deployment.unit."intranet.war".DEPENDENCIES: WFLYSRV0153: Falha ao processar a fase DEPENDENCIES do deployment "intranet.war"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:154)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: WFLYJPA0061: O unitName persistente não foi especificado e existem definições de unidade persistentes 2 na implantação do aplicativo deployment "intranet.war". Você pode tanto alterar a implantação do aplicativo para possuir apenas uma definição da unidade de persistência ou especificar o unitName para cada referência de uma unidade de persistência.
at org.jboss.as.jpa.container.PersistenceUnitSearch.ambiguousPUError(PersistenceUnitSearch.java:187)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinDeployment(PersistenceUnitSearch.java:153)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findPersistenceUnitSupplier(PersistenceUnitSearch.java:75)
at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:64)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getPersistenceUnit(JPAAnnotationProcessor.java:372)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getBindingSource(JPAAnnotationProcessor.java:296)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processField(JPAAnnotationProcessor.java:171)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processPersistenceAnnotations(JPAAnnotationProcessor.java:138)
at org.jboss.as.jpa.processor.JPAAnnotationProcessor.deploy(JPAAnnotationProcessor.java:100)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:147)
... 5 more
17:37:12,720 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Falha na operação ("deploy") - endereço ([("deployment" => "intranet.war")]) - falha na descrição: {"WFLYCTL0080: Falha de serviços" => {"jboss.deployment.unit.\"intranet.war\".DEPENDENCIES" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"intranet.war\".DEPENDENCIES: WFLYSRV0153: Falha ao processar a fase DEPENDENCIES do deployment \"intranet.war\"
Caused by: java.lang.IllegalArgumentException: WFLYJPA0061: O unitName persistente não foi especificado e existem definições de unidade persistentes 2 na implantação do aplicativo deployment \"intranet.war\". Você pode tanto alterar a implantação do aplicativo para possuir apenas uma definição da unidade de persistência ou especificar o unitName para cada referência de uma unidade de persistência."}}
17:37:12,752 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Implantado "intranet.war" (runtime-name: "intranet.war")
17:37:12,753 INFO [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Relatório
de status de serviço WFLYCTL0186: Serviços que falham na inicialização: service jboss.deployment.unit."intranet.war".DEPENDENCIES: org.jboss.msc.service.StartException in service jboss.deployment.unit."intranet.war".DEPENDENCIES: WFLYSRV0153: Falha ao processar a fase DEPENDENCIES do deployment "intranet.war"
O persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="intranet" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/intraDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
<persistence-unit name="intrasis" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/intrasis</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
public abstract class GenericDAO<T> implements Serializable {
private static final long serialVersionUID = -4843781544193464186L;
private static Logger logger = LogManager.getLogger(GenericDAO.class);
protected Class<T> classe;
@PersistenceContext
protected transient EntityManager entityManager;
protected GenericDAO(final Class<T> classe) {
this.classe = classe;
}
public T save(final T entity) {
try {
entityManager.persist(entity);
entityManager.flush();
} catch (Exception e) {
logger.error("Falha ao persistir a entidade: " + entity.toString(), e);
throw TypeApplicationException.PERSISTENCIA.buildException("Ocorreu uma falha na persistencia");
}
return entity;
}
the GenericDAO class
public abstract class GenericDAO<T> implements Serializable {
private static final long serialVersionUID = -4843781544193464186L;
private static Logger logger = LogManager.getLogger(GenericDAO.class);
protected Class<T> classe;
@PersistenceContext
protected transient EntityManager entityManager;
protected GenericDAO(final Class<T> classe) {
this.classe = classe;
}
public T save(final T entity) {
try {
entityManager.persist(entity);
entityManager.flush();
} catch (Exception e) {
logger.error("Falha ao persistir a entidade: " + entity.toString(), e);
throw TypeApplicationException.PERSISTENCIA.buildException("Ocorreu uma falha na persistencia");
}
return entity;
}
the class itemDAO that should use the intrasis base. In it I tried to use @PersistenceContext (unitName="intrasis")
public class ItemDAO extends GenericDAO<Item> {
private static final long serialVersionUID = 1L;
@PersistenceContext(unitName="intrasis")
protected transient EntityManager entityManager;
protected ItemDAO() {
super(Item.class);
}
}
another class that uses the other bank and tbm I tried the PersistenceContext for the other bank.
public class CursoDAO extends GenericDAO<Curso> {
private static final long serialVersionUID = 5659645428769766934L;
@PersistenceContext(unitName="intranet")
protected transient EntityManager entityManager;
protected CursoDAO() {
super(Curso.class);
}
public List<Curso> buscarTodos() {
String jpql = "SELECT e FROM Curso e";
TypedQuery<Curso> typedQuery = entityManager.createQuery(jpql, Curso.class);
return typedQuery.getResultList();
}