the force that pushes object up
Random header image... Refresh for more!

JPA Implementation Patterns

These days I am working on Human Task implementation for WSO2 Carbon platform, and decided to use JPA based persistence mechanism. While working on ‘Task Engine’ implementation, I found out that JPA is not just about implementing set of entities and persisting them using ‘Entity Manager’. I found out that, there are small but very important things that anyone should know to make their JPA based implementations successful. Without those you’ll stuck in the middle of some thing, deciding what is the best option. While googling I found series of articles from Vincent Partington on JPA implementation patterns which are very useful for anyone who new to JPA. In this post I am going to summarize each and every post from ‘JPA Implementation Patterns‘ article series.

Data Access Objects

This post starts with the background information about JPA and JPA implementations. After that Vincent tells us that there are more things than data access object and domain objects, like transaction handling, lazy loading and detach objects.

Then he comes to the main topic and first describes why we need DAOs(Data Access Objects) and how DAO layer which resides on top JPA can help you. After that he provides us with the information about type-safe generic DAO pattern, how to use DAO and advantages of type-safe generic DAO pattern.

In think this post is a must read for anyone who try to get started with JPA and this post help me a lot to understand issues in my JPA based implementation.

Bidirectional Associations

This post is about association between JPA entities. And it mainly describes a bidirectional association pattern used by developers who use JPA. Vincent start this post with a sample and then describes how bidirectional association can make developers life easier. After that he describes problems associated with bidirectional association and provide reader with a patter that can use to solve the issues. At last he describes the variations to the patter the introduced and what user must consider before using this pattern.

Saving (detached) Entities

This post talks about saving detached entities or merging changes done to the detached entities. First Vincent talked about how entities become detached and what  will happen when try to persist detached entity.

Then he describes about differences between Hibernate’s saveOrUpdate and JPA Entity Manager merge method.

In the last part of this post Vincent describes the problems with JPA merge and a pattern which every one can use to solve issues described with merge method.

Retrieving Entities

This post talks about two ways of retrieving an entity. One is using EntityManager.find and other is using JPQL queries. This post contains three example ways of retrieving entities by using EntityManager.find and JPQL queries.

Removing Entities

This post mainly focused on removing entities with bi-directional relationships. And Vincent has described pattern to overcome issue with removing entities when there are bi-directional associations.

Service Facades and Data Transfer Objects

This post is mainly about applying old school enterprise application architecture patterns to your JPA base implementations. First Vincent describes why we must bother about DAO’s DTO’s and Service Facades. Then he describes pros and cons of Data Transfer Objects(DTO) and pros and cons of Service Facades in JPA space. Finally he explain to you implications to your JPA architecture when you use DTO’s and Service Facades.

Lazy Loading

To use JPA to its full potential you must understand ‘Lazy Loading’ and how it works. Understanding lazy loading will allow you to utilize computing resources effectively and help you to design you entities and their relationships in way that it increase the efficiency.

First part of the post is about lazy loading and when lazy  loading occur. Then post has some information about lazy loading implementation mechanisms. After that the post describes “Runtime proxy based lazy loading” in hibernate and “Runtime bytecode instrumentation” in OpenJPA. Then there is a seperate section on differences between Hibernate and OpenJPA. Finally post contains a patter which help us to effectively use lazy loading.

January 30, 2010   No Comments