Contact Us

Contact Us



Pleas confirm by checkbox


Technical

Designing the Persistence Layer

Author_img
By talentica July 09, 2013
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!
Related posts
3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks
Technical

3 Crucial Ways of Inserting Code into a Running Application without Creating Bottlenecks

By talentica January 28, 2022
Apache Spark Standalone Setup On Linux/macOS
Technical

Apache Spark Standalone Setup On Linux/macOS

By talentica October 20, 2021
Apache Flink Standalone Setup on Linux/macOS
Technical

Apache Flink Standalone Setup on Linux/macOS

By talentica October 13, 2021
Identity, Authentication, And Access Management Using Azure Active Directory  
Technical

Identity, Authentication, And Access Management Using Azure Active Directory  

By talentica September 22, 2021
Things to Know Before You Select A Crypto Wallet
Blockchain

Things to Know Before You Select A Crypto Wallet

By talentica September 20, 2021
Solve 3 Most Irritating Outlook Email Rendering Issues.
Technical

Solve 3 Most Irritating Outlook Email Rendering Issues.

By talentica September 15, 2021
Intuit Wasabi – A Scalable A/B Testing Solution
Technical

Intuit Wasabi – A Scalable A/B Testing Solution

By talentica September 01, 2021
How To Pick The Right Data Analytics Strategy For Serverless Systems?
Big Data

How To Pick The Right Data Analytics Strategy For Serverless Systems?

By talentica August 25, 2021
Change Notifications and Named Options using Options pattern in .NET Core
Technical

Change Notifications and Named Options using Options pattern in .NET Core

By talentica August 18, 2021
Create Strongly Typed Configurations in .NET Core
Technical

Create Strongly Typed Configurations in .NET Core

By talentica August 13, 2021

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.