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 4 of 4

 InputStream in; 
// Initialize the input stream.
Person p = Util.deserialize (in);

// ... ... em.merge (p);

// p is a managed object now. Any change to p // is automatically detected and persisted. p.setName ("Another Name");


Database synchronization

When the EntityManager object is used in a session bean, it is tied with the server's transaction context. The EntityManager commits and synchronizes its contents to the database when the server's transaction commits. In a session bean, the server transaction commits at the end of the call stack by default. Of course, you can also specify the detailed transactional properties of each business method via annotations. The example below shows how to declare a new transaction for a session bean method:

 @TransactionAttribute(TransactionAttributeType.REQUIRESNEW)
public void update () {
  // Update Person objects in this method
  // and all updates are committed and flushed
  // to the database at the end of this method.
}  



Flushing database operations in a batch
To only flush changes to the database when a transaction commits, the container can group database operations in a batch and reduce the expensive database roundtrips.


If you need to flush the updates to the database before the transaction commits, you can call the EntityManager.flush() method explicitly. Or you may tag a method with the @FlushMode(FlushModeType.NEVER) annotation, and the transaction manager will not flush the updates to the database at the end of this method (i.e., the end of the transaction). In this case, you can manually flush all database updates to achieve the most control.

In conclusion

EJB 3.0 provides a simple and effective framework for mapping Java POJOs to relational tables in SQL databases. It uses sensible default mapping strategies based on the structure and naming of the Java class. Yet you can also override any defaults and handle complex object relationships using a simple set of annotations.

The EJB 3.0 EntityManager provides simple APIs to persist, find, and search objects from the database. Each EntityManager object is associated with a set of mapped POJOs and has its own database settings. It is also automatically tied to the application server's transaction manager.

About the author

Dr. Michael Yuan works for JBoss. He specializes in end-to-end enterprise solutions. He is the author of three books, Nokia Smartphone Hacks, Enterprise J2ME, and Nokia Series: Developing Scalable Series 40 Applications. Yuan received a Ph.D. degree from the University of Texas at Austin.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback

Resources