Java Reference
In-Depth Information
<persistence-unit name=" sampleManager " transaction-type="RESOURCE_LOCAL">
<class>com.hibernatebook.advanced.Sample</class>
<properties>
<property name="hibernate.connection.driver_class"
value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url"
value="jdbc:hsqldb:file:/advanced/db/advanceddb;SHUTDOWN=true"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.pool_size" value="0"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
The configuration file can contain multiple named <persistence-unit> elements, each
corresponding to a different configuration of the ORM environment. In the example in
Listing A-1, we have created a single annotation-mapped entity. The <properties> element
then configures the implementation-specific (i.e., Hibernate-specific) properties. In
Listing A-1, we have configured a database connection and dialect. When configuring a J2EE
environment, the connection would usually be provided through generic elements of the
persistence unit; Listing A-2 shows a configuration that takes advantage of this approach.
Listing A-2. An EJB 3 persistence.xml Configuration File Using a JTA Data Source
<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_1_0.xsd"
version="1.0">
<persistence-unit name=" sampleManager " transaction-type="JTA">
<jta-data-source>java:comp/env/jdbc/advanced</jta-data-source>
<class>com.hibernatebook.advanced.Sample</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Listing A-2 shows that the configuration file requires very little Hibernate-specific infor-
mation. In a J2EE environment, it is possible at deployment time to substitute alternative
EJB 3 providers for the providers indicated in the application's metadata.
In a J2SE environment, the configuration information is accessed by creating an
EntityManagerFactory class by calling the createEntityManagerFactory() method of the
Persistence class, with the configured name of the persistence unit (shown in bold in
Search WWH ::




Custom Search