Getting started with JPA and Mule

motif

Working with managed entities in Mule applications can be difficult.  Since the JPA session is not propagated between message processors, transformers are typically needed to produce an entity from a message’s payload, pass it to a component for processing, then serialize it back to an un-proxied representation for further processing.

Transactions have been complicated too.  Its difficult to coordinate a transaction between multiple components that are operating with JPA entity payloads.  Finally the lack of support for JPA queries makes it difficult to  load objects without working with raw SQL and the JDBC transport.

Mule Support for JPA Entities

The JPA module aims to simplify working with JPA managed entities with Mule.  It provides message processors that map to an EntityManager’s methods.  The message processors participate in Mule transactions, making it easy to structure JPA transactions within Mule flows.  The JPA module also provides a @PersistenceContext implementation.  This allows Mule components to participate in JPA transactions.

Installing the JPA Module

To install the JPA Module you need to click on “Help” followed by “Install New Software…” from Mule Studio.  Select the “MuleStudio Cloud Connectors Update Site” from the “Work With” drop-down list then find the “Mule Java Persistence API Module Mule Extension.”  This is illustrated below:

Installing the JPA Module in Mule Studio

Fetching JPA Entities

JPA query language or criteria queries can be executed using the “query” MP.  Supplying a statement to the query will execute the given query and return the results to the next message processor, as illustrated in the following Gist:

Could not embed GitHub Gist 3285863: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

The queryParameters-ref defines the parameters.  In this case  the message’s payload as the parameters to the query.  The following query illustrates how a Map payload could be used to populate query parameters:

Could not embed GitHub Gist 3285880: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

The query processor also supports criteria queries by setting the queryParameters-ref to an instance of a CriteriaQuery, as illustrated in the functional test snippet below.

Could not embed GitHub Gist 3285898: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

You can use the  “find” MP to load a single object if you know its ID:

Could not embed GitHub Gist 3286544: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

Transactions and Entity Operations

The default behavior of most JPA providers, like Hibernate, is to provide proxies on entity relationships to avoid loading full object graphs into memory.  When these objects are detached from the JPA session, however, attempts to access relations in the object will often fail because the proxied session is no longer available.  This complicates using JPA is Mule applications as JPA objects pass between message processors and inbetween flows and the session subsequently becomes unavailable.

The JPA module allows you to avoid this by wrapping your operations in a transactional block.  Let’s first look at how to persist an object then query it within a transaction.  The below assumes the message’s payload is an instance of the Dog domain class.

Could not embed GitHub Gist 3286592: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

Now let’s see how we can use the merge processor to attach a JPA object to a new session.  This can be useful when passing a JPA entity from one flow to another.

Could not embed GitHub Gist 3286613: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

Detaching an entity is just as simple:

Could not embed GitHub Gist 3286620: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

Component Operations with JPA

The real power of using JPA with Mule is allowing your business services to participate in Mule managed JPA transactions.   A @PersistenceContext EntityManager reference in your component class will cause Mule to inject a reference to a transactional flow’s current EntityManager for that method, as illustrated in the following class:

Could not embed GitHub Gist 3286681: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

We can now wire the component up in a flow:

Could not embed GitHub Gist 3286689: Bad credentials. The API can't be accessed using username/password authentication. Please create a personal access token to access this endpoint: http://github.com/settings/tokens

Conclusion

JPA is an important  part of the JEE ecosystem and hopefully this module will simplify your use of JPA managed entities in Mule applications.

 


We'd love to hear your opinion on this post


5 Responses to “Getting started with JPA and Mule”

  1. Hi,

    I’m running into a problem using the Mule JPA Module. I’m trying to setup Mule to write to a MySQL database using the EclipseLink JPA provider but it seems that the JPA transaction is not being committed. I’m not sure if this is a bug or if there is an issue with my config.

    Please could you take a look. I have posted details here: http://stackoverflow.com/questions/18575724/configuring-the-mule-jpa-module-to-use-eclipse-link-and-mysql

    Any idea what could cause this?

    Thanks

    • Can you turn up logging on org.mule.module.jpa to DEBUG? The JPATransaction class should log when it calls commit() on the transaction.

    • The configuration looks OK. Is the component class doing anything with the EntityManager? (ie, using @PersistenceContext)

  2. I have turned up logging to DEBUG. You can see in the link that I posted that the JPA module logs that it binds to the transaction (which in turn begins the transaction) but it doesn’t ever seem to have its doCommit() method called, so the transaction doesn’t ever get committed.

    Any idea what could cause this? Am I not setting up the transaction manager correctly?

  3. No, all it does is convert XML to an Object that is a JPA Entity. Is there anything else I can try or supply you with to help see what is going on here?