Designing the Persistence Layer

The trend in recent years is to model the entities in application’s object model and to use the database for storing the persistence information defined in the entity layer. This helps you create loosely coupled application components, richer relationship between objects, support various databases etc. With tools like Hibernate, it is really easy to map an object model into a relational database schema.

Defining entities for persistence layer is similar to defining any object model. Look for common properties that all objects can share. While defining the entities, use OOPS concepts like inheritance and polymorphism.

The focus should be to achieve the following:

  1. Avoid code duplication in the persistence layer
  2. Implement common functionality in the domain
  3. Audit each and every change properly
  4. Handle concurrency to avoid data loss/data getting overridden
Here are some of the best practices:

Base Class for Common Fields 
Create base class containing fields like id, version, timestamp and other common fields. Use inheritance to avoid code duplication. Also use JPA annotations wherever possible.

Polymorphism
Have a hierarchy of classes to be used while defining the object model. There will be cases when one base class is not enough. On the other hand, you can have entities which might not require all the fields defined in the base class.

DAO Pattern
Encapsulate logic for accessing data in an object or set of related objects. The goal should be to have database related logic in the DAO classes.

Dependency Injection
Query data by injecting the entity class that the DAO will be querying, along with defining the generic type.

Pagination
Write a generic implementation to support pagination. The goal should be to push pagination all the way down to the database and reduce resource consumption.

Auditing
Add audit fields/annotations to the objects which require auditing. This helps you keep track of objects- when are they created, who created them, who modified the object and when.

Lazy Loading
Use this when you do not want to query all data from an association when an object is loaded.

Eager Loading
Mark the associations with FetchType EAGER incase the association is needed mandatorily.

Querying Associations
Use Lazy loading in general, but if you need to load the association at query time, you can set the query params to make the association as Eager.

Persistence Layer with Behavior
Let persistence layer own the behavior of entities. It means that apart from creating the object graph you should write code to operate on the domain objects.

In my next post I will cover how Domain Driven Design can be used to achieve this easily.

Concurrency
Use optimistic concurrency control with versioning. You can use version numbers, or timestamps, to detect conflicting updates and to prevent lost updates. If you don’t configure Hibernate to use optimistic locking, it uses no locking at all. So, in this case the last update always wins.

Hope you enjoy designing persistence layer, keeping these key points in mind!

4 thoughts on “Designing the Persistence Layer

  1. SDK July 21, 2013 / 6:52 am

    good to use Repository Pattern…
    Hope we enjoy this one as well…

    Like

  2. Vaibhav Kamble July 22, 2013 / 12:39 pm

    If your DB modeling is close to perfect, you can simply generate JPA classes along with DAO layer using Netbeans and Eclipse generation tools.

    I personally prefer Netbeans for this task as I found it to be having precise control over lot of things.

    Cheers,
    Vaibhav K

    Like

  3. Ranjan Kumar July 29, 2013 / 10:34 am

    Generating classes is good to get started with. Apply these rules after that to get the best of what these tools/frameworks provide.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s