Sunday, July 20, 2008

Hibernate or JPA

Back to java. Busy time in future?

I have some works to be finished, new project what just starting for my new client and waiting for new huge projects, from few new customers. It looks I will be very, very busy.

Better solution in less time

Everyone is using hibernate for ORM, but not me. Yes I was ignoring it, since I have tried it first time 4 or even 5 years back. I always thought, why do I have to add another technology layer, introduce another dependency and make my applications dependent on technology which could not be easily removed or replaced. All my software is layered in the UI, BO and DAO layers and I never had a problem to write DAO layer classes for data manipulation with JDBC. I always felt I have more power, flexibility and options by doing it on my own.

It's still possible to write another implementation of DAO class and to have an option to replace JDBC implementation class with hibernate, but it's not as easy to do as easy to say. If you have big number of entities and relations between them, Hibernate does a lot of nice work by handling relations and lazy loading, this is a really nice feature, I like it and it's handled automatically. Problem is, once you accept features from Hibernate and start to use them, it's no way ho I can do that with JDBC. How can I make a lazy loading of linked list, I became dependent on Hibernate? The irony is, if taking hibernate road I have to change my way of thinking and application design. By doing that I'm loosing a way back, I will never be back.

The same dependency upon technology I see with spring framework. They call it Dependency Injection pattern and say that you components are loosely coupled, that's fine. But you have to be very careful, it's just amazing how fast you can be dependent on spring. The technology that is sold as a way to make you components independent makes yours application design dependent on spring. What is a difference, to have independent class and dependent application. Fine, I know there are some benefits but they can write in red "By making your classes and components independent on each other you making your application dependent on spring. And you are in danger to make yourself dependent on Spring. Please don't overuse it".

JPA may be a better choice

Lately since I moved my development to Glassfish and EJB3 I'm started to use JPA yes the same thing, but with important differences. The choice was obvious either JPA or CMB. CMB is too old, inefficient and technology dead end. JPA still new but annotation base with different implementations and it's specification based, jsr.

Why now?

I'm going to sacrifice somthing in order to get a better productivity, time spend on project development. I'm not happy how much time I spend on DAO layer. Yes the main reason is time, no less, no more just time. I hope I will get some advantages and will be able to improve time.

I still have to make some decisions where to go JPA or Hibernate. It looks that JPA could be better for me, but still have to decide.

4 comments:

Anonymous said...

Maybe you should write your own JPA implementation? that way you wont need to add another dependency(dont do it at independent project)

Remis B said...

Nice irony :) . There are already plenty of different JPA implementation as I know at least 5 of them and as minimum 3 of them are free and open source. I do prefer to use JPA just for that, but at current time they are not function complete and Hibernate is more powerful as JPA. There we come back to the same question. Should I use JPA or some other ORM and fall back to JDBC when I need something more advance?

Anonymous said...

from what i know jpa is subset of hibernate. with hibernate you can still execute native sql queries(i dont know if jpa also have this). what i like the most about ORM is that you can write all the joins once and then just include it as collections. that saves a lot of time.

Remis B said...

Yes that's right JPA is a subset of Hibernate. You are allowed to use regular SQL queries with either Hibernate and JPA. In case you are going to use SQL queries you have to do additional clunky mapping between SQL resulting data and JPA. Here is some docs regarding that http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#ColumnResult

The main question is still unanswered. Is it worth to change application design, become dependent on Hibernate or JPA and to become dependent on these technologies? What if it does not work as expected in all cases. You ended with some mess, here I use JPA and this part is done differently in JDBC style.

I guess, the way to go is, do you work, have it done and care less what you will get at the end. How many people does not pay attention to these little and important things.