Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Simplify enterprise Java development with EJB 3.0, Part 2

POJO persistence made easy

  • Print
  • Feedback

Page 3 of 4

A .par file is a jar file of the entity bean classes together with a simple configuration file META-INF/persistence.xml. The persistence.xml file defines the name of this persistence context. It tells the EJB 3.0 container which backend database (DataSource) to use for this set of entity beans. The persistence.xml file also contains implementation-specific configuration properties. For instance, JBoss EJB 3.0 is implemented on top of Hibernate 3.0. So you can pass any Hibernate configuration options in the persistence.xml file. Here is an example persistence.xml file with JBoss- and Hibernate-specific configuration properties on the SQL dialect and the second-level cache:

 <entity-manager>
  <name>cal</name>
  <jta-data-source>java:/DefaultDS</jta-data-source>
  <properties>
    <property name="hibernate.dialect"  
            value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.cache.provider_class"
            value="org.jboss.ejb3.entity.TreeCacheProviderHook"/>
    <property name="hibernate.treecache.mbean.object_name"
            value="jboss.cache:service=EJB3EntityTreeCache"/>
  </properties>
</entity-manager>



The EntityManager

Once you have the entity beans deployed, you must access and manipulate them via the EJB 3.0 EntityManager API. The EJB 3.0 container provides one EntityManager object for each deployed persistence context (i.e., the .par file). From an EJB 3.0 session bean POJO (see Part 1), you can inject the EntityManager object via the @PersistenceContext annotation and pass in the name of the context:

 @Stateless
public class ManagerBean implements Manager {

@PersistenceContext (unitName="cal") protected EntityManager em; // Use "em" // ... ... }


Basic operations

To create a new data object and save it to the database, you can simply use the Java new keyword to create the POJO and pass it to the EntityManager.persist() method:

 Person p = new Person ();
p.setName ("A new baby");
p.setDateOfBirth (new Date ());
em.persist (p);



To retrieve objects from the database, you can use the EJB 3.0 Query Language to search the database. The following example shows how to get all rows in the Person database table to return as a collection of Person Java objects:

 // Get all persons
Collection <Person> persons = (Collection <Person>) 
    em.createQuery("from Person p").getResultList();



Managed POJOs

The objects saved and retrieved by the EntityManager are managed in the persistence context. That means if the objects change later, the changes are automatically detected and persisted to the database. In the following example, we update a property of a managed POJO. The change is automatically detected by the EJB 3.0 container and sent to the database.

 Person p = em.find(Person.class, personId);
p.setName ("Another Name");

// p is automatically updated to the database // at the end of the current transaction. // No additional API calls.


Since EJB 3.0 entity beans are just POJOs, they can be serialized and passed over the network. If an object is not created by the container (e.g., it is passed in from a network connection or is a return value from a remote procedure call), the persistence context does not manage it. You can merge a nonmanaged POJO into the persistence context by calling the EntityManager.merge() method. Below is an example of merging a deserialized POJO into the current persistence context:

  • Print
  • Feedback

Resources