Enterprise Java Development@TOPIC@
Physical connections to database are placed into a pool
Logical connections are borrowed and returned from the pool
Figure 96.1. Server SQL DataSource (standalone.xml)
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
No connection information - use jta-data-source
Properties included define how to work with DB -- not how to connect
Figure 96.2. Server-side persistence.xml
<?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="ejbjpa-hotel">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
...
<properties>
<property name="hibernate.dialect" value="${hibernate.dialect}"/>
...
</properties>
</persistence-unit>
</persistence>
Figure 96.3. EJB persistence.xml Placement
enc-config-example-ejb |-- ejava | `-- ejb | `-- examples | `-- encconfig | |-- dto | | `-- NCPair.class | `-- ejb | |-- AnnotatedEJB.class | |-- InjectedEJB.class | |-- JNDIReader.class | |-- JNDIReaderRemote.class | `-- XMLConfiguredEJB.class `-- META-INF `-- persistence.xml
META-INF/persistence.xml
Same location as normal JAR
Figure 96.4. WAR persistence.xml Placement
ejb-jpa-example-war |-- META-INF `-- WEB-INF |-- classes | |-- info | | `-- ejava | | `-- examples | | `-- ejb | | `-- ejbjpa | | |-- dto | | | |-- FloorDTO.class | | | `-- RoomDTO.class | | `-- ejb | | |-- HotelInitEJB.class | | |-- HotelInitRemote.class | | |-- HotelMgmtEJB.class | | |-- HotelMgmtLocal.class | | |-- HotelMgmtRemote.class | | |-- ReservationEJB.class | | `-- ReservationRemote.class | `-- META-INF | |-- ejb-jar.xml | |-- jboss-ejb3.xml | `-- persistence.xml `-- lib `-- ejb-jpa-example-blimpl-4.0.0-SNAPSHOT.jar
WEB-INF/classes/META-INF/persistence.xml
Inside JARs within WEB-INF/lib also allowed
to have persistence.xml reference @Entity class(es) outside of local archive
Figure 96.5. Reference External @Entities: EAR Deploy
ejbsessionBankEAR-4.0.0-SNAPSHOT |-- ejbsessionBankEJB.jar |-- ejbsessionBankWAR-4.0.0-SNAPSHOT.war |-- lib | |-- ejava-util-4.0.0-SNAPSHOT.jar | `-- ejbsessionBankImpl-4.0.0-SNAPSHOT.jar `-- META-INF `-- application.xml
<?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="ejbsessionbank">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<jar-file>lib/ejbsessionBankImpl-4.0.0-SNAPSHOT.jar</jar-file>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect"/>
...
</properties>
</persistence-unit>
</persistence>
jar-file element able to reference external JAR using a deterministic, portable path in EAR
jar-file element not usable in WARs
Figure 96.6. Reference External @Entities: WAR Deploy
<?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="ejbjpa-hotel">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>info.ejava.examples.ejb.ejbjpa.bo.Guest</class>
<class>info.ejava.examples.ejb.ejbjpa.bo.Room</class>
<class>info.ejava.examples.ejb.ejbjpa.bo.Floor</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect"/>
...
</properties>
</persistence-unit>
</persistence>
There is no need to declare "class" entity references into a properly configured "jar-file" that is located in the classpath of a deployed EAR. Use "class" entity references when using a WEB-INF/classes/META-INF deployment of a WAR -- or if you really need to only include specific entities in the archive.