Java Reference
In-Depth Information
Persistence unit transaction type —In a Java SE application, the default transaction type is
RESOURCE_LOCAL , while in a Java EE environment, the transaction type is
JTA . This means
A
that the entity manager participates in the transaction.
Provider —This element identii es the class that provides the factory for creating the
EntityManager instance.
Class —The entity classes used in the application should be listed in the class element.
Property —Additional properties can be specii ed, such as database connection properties and
persistence provider properties like options to drop‐create new tables.
The EntityManager is associated with a persistence context that is dei ned in the persistence.xml
in Listing 12‐5.
LISTING 12‐5: The 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="moviePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/sample</jta-data-source>
<class>com.devchronicles.dataaccessobject.Movie</class>
</persistence-unit>
</persistence>
The specii c data source is dei ned in this persistence.xml i le. In this case, you have dei ned
a Derby database using the eclipse link provider. You have dei ned the transaction type as JTA
because this is a Java EE application implementation, and you have specii ed the class entity to be
com.devchronicles.dataaccessobject.Movie .
Finally, you need to inject the DAO that you have created and use it. The client in Listing 12‐6 gets
an instance of the DAO injected and uses it to retrieve all movies.
LISTING 12‐6: The client
package com.devchronicles.dataaccessobject;
import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;
@Stateless
public class Client {
@Inject
continues
Search WWH ::




Custom Search