Thursday, 13 April 2017

From ORM to Spring Data



First of all let us understand what is Object Relational mapping(ORM). ORM is a technique to map java objects to tabular data. Here tabular data means our relational database. When it comes to an object, it has inheritance property, object entity(==), object equality(equals) and many others which creates a mismatch while dealing the same object with a tabular data format. In tabular format, we have a table. A table has many rows and columns. We can have an object that contains another object and like this a huge chain of objects. But in from relational database perspective we don't have one table containing another. But we have joins and union ro deal with multiple tables using SQL.
Whatever the mismatch between object representation of data and tabular data, it is required to process those tabular data and implement logic on top of that in object oriented programming language and deliver the meaningful result to clients. ORM comes into picture when we try to deal with the tabular format of data from object persective. ORM is a tool that provides mapping between the tabular data format and object representation of data.
Different ORM Implementations
Now question comes what are the different implementations of ORM meaning what are the tools for object relational mapping. Before that we require a specification, a standard rules defined that these tools follow to be an ORM implementation. While dealing with specification, JPA comes into picture. JPA stands for Java Persistence API. It provides specification for the ORM implementations. There many imlemmentaations of ORM such as Hibernate(most widely used), Open JPA, eclipse link etc. Google can answer many others.
While dealing with hibernate, you need to define some configurations in a xml file and use some annotations and start writing your DAO layer with object perspective. If you are using Spring, then Spring provides nice integration with hibernate. Even if you integrate hibernate with Spring there are many boiler plate codes required to execute a simple select query. Though there are many helpers method provided by hibernate to perform different operations on DB, but still boiler plate codes are required in all the implementations.
To get rid of this boiler plate code, Spring came up with Spring data.Let us discuss about spring data features.
Features:
Sophisticated support to build repositories based on Spring and JPA
Support for Querydsl predicates and thus type-safe JPA queries
Transparent auditing of domain class
Pagination support, dynamic query execution, ability to integrate custom data access code
Validation of @Query annotated queries at bootstrap time
Support for XML based entity mapping
JavaConfig based repository configuration by introducing @EnableJpaRepositories.

Spring data also provides integration with hibernate. If you dont want to go with JPA , you can also have your hibernate implementations. To get started with spring Data JPA, you can take an example here - http://www.devglan.com/spring-boot/spring-data-jpa-example

Though you are free to use any database like mysql, oracle or sql server, spring also provides convenient ways to deal with in memory databases such as H2 during development phases. You can find an example here - spring boot h2 database example

This is a very short introduction of ORM in java world.

Sunday, 12 March 2017

Disadvantages of Hibernate


Hibernate ORM (Hibernate in short) is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database.
There are basically one to one mapping, one to many mapping and many to many mapping that eases the development. But there are many disadvantages of hibernate.

In hibernate, everything is returned as an object, and you have to fetch the entire row - NO. We can fetch single columns or some selected columns. This data can be either populated in the entity bean itself, or can be returned as a map with the key as column name. If fetching single column, no need of entity or map.

It takes more time that JdBC? NO. If your application is huge, and you have a huge database, and you make a lot of queries- jdbc has overheads, of closing and opening connections, managing transactions. Hibernate handled everything. It also caches data.

Join is not possible. NO. If you create the entity properly as per the joins required, it pretty much handles all the scenarios. You can create multiple entities for same table, so 1 entity can be created without the join, for the queries which don't require joins, and other entity can be used for queries which need a join.

If you you have a small set of tables, probably you may not notice the difference, but in huge applications hibernate is a plus. It helps cache data, thereby reducing response time, it manages the transactions and you don't need to worry much about them, and you don't have to bother about making connection and closing connection every time you write a query in your code.

Also we can not ignore the hibernate inheritance while discussing about hibernate features.

 Performance Cost

Hibernate generates lot of SQL statements in runtime based on our mapping , so it is bit slower than JDBC.

If we use JDBC, then we directly write our queries, so no extra effort is required.

Does not allow multiple inserts

Hibernate does not allow some queries which are supported by JDBC.

For example, It does not allow to insert multiple objects (persistent data) to same table using single query.

Developer has to write separate query to insert each object.

More Complex with joins

If there are lot of mappings and joins among the tables, then code becomes bit complex to understand as we need to define the mapping and join information in the XML file.

Poor performance in Batch processing:

It advisable to use pure JDBC for batch processing as Hibernate performance is not good in Batch processing.

Not good for small project.

Small project will have less number of tables , introducing entire Hibernate framework will be overhead than useful.

Learning curve

As many other tool, Hibernate also takes considerable amount of time and effort to learn. I can’t say it is 100% disadvantage but yes it’s not so easy to learn.

So because of steep learning curve, we may consider it as disadvantage.

Performance : A lot is being talked about hibernate performance. We have used Spring and hibernate with one of the biggest deployment in clinical applications and I can tell you, I have not seen any issues or so because of hibernate.

Yes we had to fix the HQLs at few places but that is a normal tuning process.

Lots of API to learn: A lot of effort is required to learn Hibernate. So, not very easy to learn hibernate easily.

Debugging: Sometimes debugging and performance tuning becomes difficult.

Slower than JDBC: Hibernate is slower than pure JDBC as it is generating lots of SQL statements in runtime.

Not suitable for Batch processing: It advisable to use pure JDBC for batch processing.

Not suitable for Small projects : For small project having few tables it is useless to work with hibernate.

Does not allow multiple inserts : Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.

Generates complex quires with many joins : For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion.

Everything is returned as object : Yes that is true and even if you would like to get an id or name, you would write the hql and get an object which has the complete data set. This is anyways not bad because while designing methods, you will have coarse grained objects which well be returned. I would not prefer to return String or int from my methods.
For more topic on hibernate visit here - Hibernate Tutorials