JPA implementation patterns: Wrap-up

The previous blog in the JPA implementation patterns series discussed different ways to test your JPA code. Figuring out how to test DAO's and then being frustrated because the existing literature on JPA seemed to say very little on this subject, was actually the trigger for me to write these blogs. I have now come full circle, which means it's time to wrap up the series. There's lots more to write about, so keep following this blog! 🙂

After discovering that there was a lack of documentation on how to use JPA in real-life scenario's, I have written a series of blogs about the JPA implementation patterns I discovered while writing JPA code. To wrap up the series, I have made an overview of all the patterns that have been discussed for easy reference. The list is mostly in chronological order. I only changed the order slightly to make a distinction between the basic patterns and the advanced patterns.

Basic patterns

Advanced patterns

I hope these patterns will help you when writing your JPA application. If there is one thing to take away from this series is that it is pays off to really look into the details of JPA. Even though the promise of an abstraction layer invites you to take ORM for granted. Incorrect database usage is still Java EE performance problem #1, so there is a lot to gain here!

While writing down my experiences I have learned a lot about JPA. First of all because writing something down forces you to really look into the details, but most importantly because of the feedback I got on the blogs and after my presentation at J-Spring, the Dutch Java developer conference. And finally I would like to thank my colleague Albert Sikkema for his contribution to the series!

Comments (15)

  1. crystal - Reply

    July 16, 2009 at 9:03 am

    i can't even open java to download (core files won't unzip) please help me.

  2. [...] falta de documentação efetiva de como usar o JPA na vida real, Vincent Partington documentou uma série de patterns com o objetivo de ajudar e direcionar os projetistas a elaborem suas camadas de persistencias [...]

  3. J Aaron Farr - Reply

    July 17, 2009 at 6:48 pm

    where can i learn advanced patterns in java

  4. Ramkumar - Reply

    August 24, 2009 at 1:35 am

    I found the JPA presentation in Dutch very useful introduction to JPA. Thought it was too useful to remain in Dutch and used Yahoo's Babel Fish service to translate it into English. Here is the URL of my attempt:

    (Original in Dutch):

    Translation in English (as a Google Doc):

  5. Vincent Partington - Reply

    August 24, 2009 at 7:12 pm

    @Ramkumar: Thanks for the translation. Yahoo Babel Fish even seems to a pretty descent job of it. 🙂

  6. JJ - Reply

    October 27, 2009 at 6:09 am

    Vincent, have you/will you create an FOS project that has the core classes and other ones to support the ideas you've presented? Would be good... 🙂

    From a little searching, I saw projects create their own similar base classes within themselves (even FOS ones), and even one that tries to be stand alone (is nice, but has some goofy config needs that cause concern for me). It is uncoordinated.

    Would be good to create a simple clean one.

  7. Vincent Partington - Reply

    October 31, 2009 at 11:05 am

    @JJ: No, I have not created a project to bundle all this code. I don't really think there is that much code to bundle... But please feel free to this so in my stead. By publishing the code I have placed it in the public domain already anyway. 😉

  8. [...] JPA implementation patterns: Wrap-up | Xebia Blog (tags: JPA patterns) [...]

  9. ecapot - Reply

    December 11, 2009 at 3:50 pm

    Is there any JPA implementation pattern for storing internationalized data?
    I found some solutions for hibernate


    but nothing for JPA only.

    Perhaps someone can post suggestions or maybe JPA 2.0 solutions?

  10. JPA Implementation Patterns | Upthrust - Reply

    January 30, 2010 at 12:29 pm

    [...] for anyone who new to JPA. In this post I am going to summarize each and every post from ‘JPA Implementation Patterns‘ article [...]

  11. Jochen Szostek - Reply

    February 9, 2010 at 11:37 am

    Awesome set of interesting articles!

  12. Brett Lindsley - Reply

    February 8, 2011 at 2:42 pm

    Very good articles; however, one issue that was not covered was dealing with exceptions. Most methods in the EntityManager throw many exceptions. Some are fatal, some are informational. One exception that is generally confusing is the OptimisticLockException. This exception indicates two transactions interfered - one was successful, the other got an exception. The exception is not fatal, thus should be retried. If the exception is thrown after several retries (maybe because of a strange execution pattern), it should be considered to be fatal.

    The solution I use is to introduce a "management layer" between the DAO and the EJB implementing the JPA calls. The management layer has the same method signatures (it is implemented by simply extending the JPA interface), and implements the retries. For example, the DAO may call the management layer for a remove. The management layer calls the JPA EJB to remove the entity. If the JPA EJB throws an exception, the management layer attempts the delete again. After several failure attempts, the mangement layer throws a checked exception to the DAO. The checked exception indicates the DAO must deal with the fact that the delete could not be completed. I usually throw this exception from the DAO to allow upper layers to take corrective action.

    A few tips on implementing this strategy. The management EJB uses the default transaction (uses the existing or creates a new one if none exists); however, the JPA EJB uses "create new" such that if the transaction fails, the management EJB can capture it and try again. In the JPA EJB, I catch all run-time exceptions and either process them (e.g. converting a not found to a null response), or, throw a checked exception. This prevents the container from pumping the log with a lot of junk about EJB exceptions.

    I found these articles interesting because I found an important use of the DAO is to access the service locator to find the EJBs. This isolates all implementation dependencies to the DAO and keeps the upper layers of code clean. It also allows switching in other DB systems (which has happened on occasion).


  13. Ted Kenney - Reply

    February 24, 2011 at 3:01 am

    For an interesting read on a similar, but alternative, approach to JPA, take a look at the article and subsequent discussion in the comments, on the eXtremeDB JNI at More information about eXtremeDB is here:

  14. raj - Reply

    December 28, 2013 at 1:58 pm

    Please explain about TransientOnbjectException.

  15. […] Partington, collègue de Xebia Hollande, a réalisé une série d’articles autour de la spécification JPA. Ses articles sont un complément à la documentation existante afin de faire le tour sur les […]

Add a Comment