jpa - Multiple persistence units in Wildfly? -
is possible have 2 persistence units in wildfly (9.0.2) application?
i "wflyjpa0061: persistence unitname not specified , there 2 persistence unit definitions in application deployment deployment "jasper-web.war". either change application deployment have 1 persistence unit definition or specify unitname each reference persistence unit."
i have unitname
specified in @peristencecontext
annotations. read somewhere disable
<!-- <subsystem xmlns="urn:jboss:domain:jpa:1.1"> <jpa default-datasource="" default-extended-persistence-inheritance="deep"/> </subsystem> -->
in standalone.xml
. removed error message, disabled injection of entitymanagers (null pointer referencing them in code)
i have tried split persistence units on 2 different ejb-jars, each own persistence.xml, they're still included in same war, wildfly still complains.
the 2 persistence units used hibernate, 1 postgresql database , 1 ucanaccess driver ms access. both work separately.
here example of works in our wildfly 8.x/9.x ejb app:
first of define classes each persistence-unit in persistence.xml, unlisted classes can turned off disable autodiscovery.
persistence.xml
<?xml version="1.0" encoding="utf-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="primary"> <jta-data-source>java:jboss/datasources/primary_ds</jta-data-source> <class>whatever.primary.model.someentity</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> ... </properties> </persistence-unit> <persistence-unit name="secondary"> <jta-data-source>java:jboss/datasources/secondary_ds</jta-data-source> <class>whatever.secondary.model.anotherentity</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> ... </properties> </persistence-unit> </persistence>
if use jboss developer studio ignore warning (it eclipse flaw):
multiple persistence units defined - first persistence unit recognized
resources.java
package whatever.util; import javax.annotation.resource; import javax.enterprise.inject.produces; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.sql.datasource; public class resources { @produces @persistencecontext(unitname = "primary") private entitymanager emprimary; @produces @persistencecontext(unitname = "secondary") private entitymanager emsecondary; @produces @resource(lookup = "java:jboss/datasources/primary_ds") private datasource dsprimary; @produces @resource(lookup = "java:jboss/datasources/secondary_ds") private datasource dssecodnary; }
dao primary example
package whatever.dao; import javax.ejb.stateless; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; @stateless public class daoprimaryexample { @persistencecontext(unitname = "primary") private entitymanager em; public void create(someentity entity) { em.persist(entity); } }
dao secondary example
package whatever.dao; import javax.ejb.stateless; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; @stateless public class daosecondaryexample { @persistencecontext(unitname = "secondary") private entitymanager em; public void create(anotherentity entity) { em.persist(entity); } }
important: if plan use booth persistence units in same transaction xa datasources should used.
Comments
Post a Comment