Why Hibernate is so much more powerful than JDBC

Should I use Hibernate for the next project? How does it compare to JDBC

This question get’s asked a lot. And the first answer is most of the times: That is comparing apples and oranges. And of course that is true. But if you are a little kid apples are round and tasty and so are oranges, so it is a reasonable question what exactly is the difference between apples and oranges?

Both (Hibernate and JDBC not apples and oranges) are used for tying application logic to your database. Hibernate is actually using JDBC. Actually you can use Hibernate in a very similiar way as JDBC. So how exactly does one work with JDBC?

You create a connection, create query from that (using SQL), change the objects (carefully keeping track of what you changed), transform you change log in SQL Statements and send those to the database (using the old or a new connection).

If you don’t like Hibernate, but are forced to use it you do it the same way:
start a session, create a query from that (using HQL), disconnect the objects, change the objects, (carefully keeping track of what you changed), start a new session and use it to store the new state of the objects in the database.

So you are saving, the DML statements for storing the objects, therefore you have to learn HQL, Hibernate, and all the weird error messages you get from it.

But you can use Hibernate very differently:

Open a session, query for some objects (using Criteria), work with these and commit the session.

That is when the real power of hibernate reveals itself. You are using neither HQL nor SQL, at least for the standard tasks. This takes away a lot of the ugly kind of error you only get when you actually run the code. You don’t have to keep track what exactly you changed. Hibernate is doing that for you. At least on the first go you don’t have to worry when to load exactly what piece of information, instead Hibernate will do it on the fly for you (and of course you will have to control it in some cases where Hibernates choices aren’t the right once).

But you can benefit from Hibernate even more: Do the design in the domain model, i.e. design you model classes the way they should look like from an OO perspective, then add the necessary Hibernate Annotations, thereby deciding on mapping strategies for inheritance and the like. In my experience this results in a richer, more expressive domain model and in a cleaner database design.

The reason for this is, that by using this approach you are bringing the power of refactoring to the database! How about an experiment. Go to your favorite database administrator and tell her that someone was ignoring the naming conventions. A couple of table names (about 70) and a couple of columns (about 600) have to be changed, oh and someone hast to change all the sql statements as well. How are your chances to get this refactoring done? Zero? Not too long ago I had exactly that kind of change as a task. Iwas done after 4 hours! It was quite easy. I did the changes to the domain model using the refactoring features of the IDE, that was it (almost). I had to change about 4 or 5 special purpose HQL Statements, and semi automatically create a change script for the database. Yes this was on an application that was installed on a client side, so I had to take care of migration scripts.

I am not saying that you should do this kind of refactoring, but how often do you refactor your database? It is still considered voodoo science but with Hibernate you can at least use a powerful IDE and doen’t have to put up with the crappy stuff you get from database tool vendors. With other tools not many people go through the trouble of changing a column name, because it is wrong in one way or the other. In Java I never stop changing a method or attribute name that I consider wrong as long as it isn’t used in some public API of course.

As usual it comes at a cost.

  • Hibernate has learning curve. People of hibernate.org claim do have learned hibernate in two weeks. I don’t think so. They might know enough to use it. But just as with SQL or Java or whatever, it takes time to really master it.
  • Hibernate is no magic pixy dust. It makes decision and uses default. If those don’t fit your needs, you might encounter problems, e.g. performance hits. In these cases you have to change the mapping, write HQL, or even SQL or do whatever needs doing to fix the problem.
  • If you are working with a legacy database or can’t use the approach described above then the benefit from using Hibernate is smaller.

If you disagree, let me know in the comments. ff you agree, let me know as well.

This entry was written by Jens Schauder , posted on Thursday November 27 2008at 07:11 pm , filed under Datenbank, Java, Softwareentwicklung . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

8 Responses to “Why Hibernate is so much more powerful than JDBC”

  • Nobody can say that an airplane is more powerful than it’s engines !

  • But every plane with more than one engine is more powerful than a single engine!.

  • Anonymous says:

    Hibernate is certainly very powerful but one question remains to be answered: how can I optimize the generated sql queries to benefit from the full power of the underlying database engine ?

    Precision: I’m talking of databases with tables having millions of rows. Having optimized queries is a necessity.

    To my knowledge :

    1 – If you use ORM tools such as Hibernate and others, you have nearly no way to optimize the text of sql queries that are sent to the db engine. Just buy new hardware with more CPUs, more RAM and store your db in cache. I do not call this “a solution” but it may be ok for you, it depends on your cash.

    2 – If you can customize the text of the sql queries, then you can unleash the power of your database engine (Oracle, SqlServer, MySql, …): they all have very powerful and very little-known capabilities such as analytical operators, cube operators and such.

    You may also decide if, and when, argument binding with JDBC is a good thing or not (eg: on Oracle, binding is considered “good” on OLTP with 100’s or 1 000’s requests per second – but it can be considered “bad” in other environments such as web frontends, DW, … because the query optimizer can not make the full use of table and index meta-data such as histograms or skew of the values stored in tables and indexes – that makes the difference between 10 seconds query run time, or sub-second run time).

    ==> Please correct me if Hibernate permits easy customization and maintenance of the sql queries sent to the database.

    For the ones who wish to know more on that subject, you may Google on “group by rollup”, “group by cube”, “group by grouping sets”, “rank”, “dense_rank”, “lag”, “lead”.

    I work with Oracle but other database engines have the same sort of extensions – this is not Oracle-specific.

    You may also read http://www.nocoug.org/download/2001-11/8i_analytical_features.ppt – this is based on a (very) old Oracle version but foundations still hold true.

    IMPORTANT: as an exercise, at each slide, try to write the same query using only what is called “standard SQL” (i.e. without analytical operators): when dealing with LAG and LEAD operators for example, or grouped running totals, you may discover new horizons.

  • You have many aproaches in optimizing the sql statements generated by hibernate: (and I assume similiar features for comparable ORM tools):

    - You can fiddle with the used dialect if you find something wrong with the generated sql on a fundamental level.
    - You can add sql snippets for calculating single attributes. Here combining analytic functions and hibernate works just great.
    - You can define custom sql statements to be used for CRUD (http://hibernate.org/hib_docs/v3/reference/en/html/querysql-cud.html) I had to look up that myself
    - you can obtain a jdbc connection and do whatever you like with it.

    I am well aware of features like rollups, cubes and analytic functions. (e.g /2008/08/20/sql-tricks-3-intervall-join/). And they sure come in handy with reporting / datawarehouse style application.

    But also in many cases the SQL skills on a java heavy team arn’t that great, so it is still beneficial to get 98% of the sql generated, working just fine. So you can use the extra time for tuning the queries, that really cause a problem.

  • One more vote for ORM,
    Use objects and relations not tables and foreign keys for more self documenting code and rapid development.

    http://www.altuure.com/2008/11/12/object-model-vs-database-schema-design/
    http://www.altuure.com/2008/10/22/why-orm-cause/

  • wow gold says:

    A merry heart goes all the way

  • dofus kamas says:

    You have many aproaches in optimizing the sql statements generated by hibernate: (and I assume similiar features for comparable ORM tools):

  • But also in many cases the SQL skills on a java heavy team arn’t that great, so it is still beneficial to get 98% of the sql generated, working just fine. So you can use the extra time for tuning the queries, that really cause a problem.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>