Object-relational mapping in Java

Object-relational mapping (ORM) is a technique which allows programmers to access a relational database from an object-oriented language and programmers do not need to map and type cast each column of database to classes manually. In other word, ORM technique take care of populating the result sets.

In Java there are various framework and libraries are available for ORM which the most famous one is Hibernate framework that offer ORM technique and more features such as transaction support, etc. However, for beginner programmers like me starting with Hibernate might be cumbersome due to various reasons and be very difficult to catch everything in the first glance.

Hence, based on my short experience I had with Hibernate, I can say it is awesome framework but little bit complicated for me as well as scary. In addition, Hibernate offers some unique features but necessary might not be used in a project. As a result, if the main intention is only using ORM, there are plenty of other libraries are available which get the job done easier and faster.

In this post, I introduce some Java libraries for ORM purpose only that can be used instead of Hibernate in quick and lightweight project.

Apache DbUtils

Apache DbUtils provides set of useful features for accessing database. The most notable function in this library is Bean Processor which has a great ability to map resultset. This is quite useful if you don’t intend to change much code, especially, if your code is written in JDBC.

Alongside, the great feature that DbUtils offer, it has the two following major drawbacks,

  1. It does not support annotation, and as a result if the column name is different from Java class member variable name, the mapping won’t be done.
  2. It does not support nested classes and result set.

For more information about Apache DbUtils, you can refer to the following links,

https://commons.apache.org/proper/commons-dbutils/

https://commons.apache.org/proper/commons-dbutils/examples.html

http://stackoverflow.com/questions/14973047/beanprocessor-partially-works

ResultSetMapper

ResultSetMapper is an old though strong library which offers annotation support and can support nested classes and relations. In comparison with Apache DbUtils, I prefer ResultSetMapper, because of having less headache in the coding and works in the same way as DbUtils works. It means that you are not required to apply much changes in your code.

The only problem that this library has, is lack of support. Since 2006, there is no update and support. It can be also be unusable soon.

For more information about ResultSetMapper, see the this link,

http://resultsetmapper.sourceforge.net/

http://resultsetmapper.sourceforge.net/examples.php

ORMLite

Undoubtedly, ORMLite is one of the best available library that offer great features. Personally, I recommend it for lightweight project. It has a very good community support and plenty example codes on the internet for different use cases are available as well. Additionally, it has support for Android platform and works perfectly with SQLite database.

The only disadvantages of this library are lack of direct SQL query support (which is not a big issue and can be done via Query Builder if you can adopt) and it requires tremendous changes for existing project to switch to ORMLite, especially, if the project is developed with a technique like JDBC.

For more info, refer to these links,

http://ormlite.com/

https://github.com/j256/ormlite-core

https://github.com/ServiceStack/ServiceStack.OrmLite

https://github.com/j256/ormlite-android

MyBatis

MyBatis is another lightweight ORM that seems promising, however, based on the first look I had to this library, I realized, it needs some in advanced configuration via XML file similar to Hibernate.

Some useful links,

https://mybatis.github.io/mybatis-3/

http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part-1.html

http://www.sivalabs.in/2012/10/mybatis-tutorial-part1-crud-operations.html

Coding your own mapper

Last but not least option can be quite exciting as well as troublesome. If you are not satisfied with any available mapper, you can go ahead and code your own mapper. It is a great experience but extremely time consuming and not suitable for enterprise applications. I recommend to do it for some fun and geeky project over a weekend or whenever you have free times. To start with, you can refer to this useful example,

http://www.codeproject.com/Tips/372152/Mapping-JDBC-ResultSet-to-Object-using-Annotations

Leave a Reply