• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HibernateUtil

 
Marcin Mogiela
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about this practice of using HibernateUtil class.

Why is such class not provided by the framewordk out of the box? On every tutorial there are descriptions of SessionFactory, Configuration, etc classes, and then HibernateUtil is introduced that hides the details. Wouldn't the api be simpler if there was only one HibernateUtil class for us to use?
Perhaps these classes gives us more flexibility but isn't it lost by having HibernateUtil class?

Thanks,
Marcin
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is such class not provided by the framewordk out of the box?
Wouldn't the api be simpler if there was only one HibernateUtil class for us to use?


There is much more to the Hibernate framework than what you find on web tutorials. For instance, there are many different ways to configure a SessionFactory, e.g. JNDI-based, annotations, XML-based, and different ways to use it, e.g. Map, XML via dom4j, POJO.

A single HibernateUtil class to handle all of these different scenarios would not be simpler than letting developer's write their own.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to mention the fact that HibernateUtil is a pretty awful name.

I too found it frustrating trying to learn Hibernate, and seeing all these references to HibernateUtil. You certainly get the impression that it is part of the Hibernate framework, that's for sure.

One thing I would say is that it's not that hard to actually create a Session from a SessionFactory yourself, so it's not too tough to create your own HibernateUtil class, or HibernateService, or whatever you want to call it. I mean, this is really the guts of a HibernateUtil class:



I'd say that's not too overly onerous for someone to code.

And what's more, the actual implementation could change quite drastically depending on the client or architecture. People need that freedom. Everythign from locking, to distributed transactions, to multithreading can impact how HibernateUtil might be implemented. It's probably a good thing to have people make a custom implementation that best suits their environment.

By the way, here's a little tutorial on creating a basic HibernateUtil class, just in case anyone is interested.

A Basic HibernateUtil Class for Hibernate3 and JPA

-Cameron McKenzie
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Camaron,

You really did a wonderful job in bringing out the book,
Hibernate Made Easy. Hats off to you. Yesterday only I got
the book from Amazon. I have completed only eight chapters.
It rocks. I certainly would rate it high above all the Head
First books. Incidentally, I am a fan of HF series. I have
nearly all of them.

Is there any idea to publish a book on Spring & SEAM.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the feedback! Seam will have to wait until after my SCEA book comes out.

Happy hibernating!

-Cameron
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic