Blog

JPA implementation patterns: Wrap-up

13 Jul, 2009

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!

guest
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
crystal
crystal
12 years ago

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

trackback

[…] 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 […]

J Aaron Farr
J Aaron Farr
12 years ago

where can i learn advanced patterns in java

Ramkumar
Ramkumar
12 years ago

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):
http://www.nljug.org/pages/events/content/jspring_2009/sessions/00027/
Translation in English (as a Google Doc):
http://docs.google.com/Doc?docid=0AZ8jw5JC8Og2ZGZmNTI4emtfMTJtZzRoYmNjag&hl=en

JJ
JJ
12 years ago

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.

trackback

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

ecapot
ecapot
12 years ago

Is there any JPA implementation pattern for storing internationalized data?
I found some solutions for hibernate
e.g. http://www.theserverside.com/tt/blogs/showblog.tss?id=HibernateInternational
but nothing for JPA only.
Perhaps someone can post suggestions or maybe JPA 2.0 solutions?

trackback

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

Jochen Szostek
12 years ago

Awesome set of interesting articles!

Brett Lindsley
Brett Lindsley
11 years ago

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).
Brett.

Ted Kenney
11 years ago

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 http://java.dzone.com/articles/java-annotations-and. More information about eXtremeDB is here: http://www.mcobject.com/extremedbfamily.shtml

raj
raj
8 years ago

Please explain about TransientOnbjectException.

trackback

[…] 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 […]

Explore related posts