SQL Zone is brought to you in partnership with:

Rob Gordon is a seasoned Java developer and a big fan of open source. Oddjob is his own open source project to make job scheduling and task automation in Java much much easier. Rob is based in London. Rob is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Hibernate – Please Don't Deprecate Yourself

03.07.2012
| 9181 views |
  • submit to reddit

There are occasional wow moments in technology and I had one such moment in the summer of 2004 when I joined a project that was using Hibernate. In a matter of hours I was reading and writing beans and it was so effortless it seemed like magic.

Today I decided to take a look at Hibernate 4.1 and for several hours I have not been saying wow but why? and what? and worse…

Starting a Hibernate project used to be easy. The first test went something like this:

Properties properties = new Properties();
properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
properties.setProperty(Environment.URL, "jdbc:hsqldb:mem:test");
properties.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
properties.setProperty(Environment.USER, "sa");
 
Configuration configuration = new Configuration()
    .setProperties(properties)
    .addClass(FeedTradePersistable.class);
 
SessionFactory sessionFactory = configuration.buildSessionFactory();

But now buildSessionFactory() is deprecated, and yet in the user guide, section 3.2 Obtaining a SessionFactory, the code sample is still:

?
SessionFactory sessions = cfg.buildSessionFactory();

I am not impressed. The Javadoc says use buildSessionFactory(ServiceRegistry) instead. But where is the code? The simple few lines to cut and paste to get me started?

I turn to Google. There are a few StackTrace questions on the subject and a Hibernate forum post that no Hibernate team member has responded to – BUT I CAN FIND NO OFFICIAL EXAMPLE! I’m beginning to think this is quite bad. The latest release of one of the most popular and depended on frameworks and it hinges on a deprecated method!

I spend a frustrating hour going round various Hibernate 4 presentations and blogs. I try something with MetadataSources but fail. More Googling and it appears this work isn’t finished yet and shouldn’t be used – however there is no comment to this affect in the JavaDoc!

I look at the unit tests and find BaseCoreFunctionalTestCase, it creates Sessions but it’s not exactly a simple example.

Eventually I go with what I found on StackOverflow:

ServiceRegistryBuilder serviceRegistryBuilder =
        new ServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(properties);
 
ServiceRegistry serviceRegistry =
        serviceRegistryBuilder.buildServiceRegistry();
 
Configuration configuration = new Configuration()
    .addClass(FeedTradePersistable.class);
 
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

It works, but is it best practice?

I feel a bit sad as I write this, I’ve lost my feel good about Hibernate feeling, and I think others will too. All I can say is, Hibernate – please don’t deprecate yourself.

 

From http://rgordon.co.uk/blog/2012/02/24/hibernate-please-dont-deprecate-yourself/

Published at DZone with permission of Rob Gordon, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Grzegorz Grzybek replied on Wed, 2012/03/07 - 12:48am

Good they didn't went the way of JavaEE/SE and hadn't created ServiceRegistryBuildeFactoryFactory with configuration in META-INF/services...

Bruno Barin replied on Wed, 2012/03/07 - 2:49am

Why don't use JPA and use hibernate as an implementation of JPA. 

I do think is a good idea to rely on the API instead of a particular library. Doing that, you could

switch to OpenJPA, without changing your code that much.

 

 

Dapeng Liu replied on Wed, 2012/03/07 - 3:35am

hibernate startup is darn slow ... the plain old JdbcTemplate still has my love!

Martin Vaněk replied on Wed, 2012/03/07 - 3:43am

I'm not suprised. Most of JBoss documentation is huge mess of deprecated information, because they are constantly rewriting everything, throwing away backward compatibility. Hibernate (pre 4) and it's integration in JBoss 7 is typical example. Just lots of unsuccesful attemts in JBoss forums without resolution. Docs/wiki are just wrong.

Sometimes informations are even misleading, when you can't figure what version it describes (mostly on wiki), because every minor/patch version number make difference. They are succesful in killing my long lasted interest in their (few years ago) succesful projects.

Today's state is that JBoss site is last place to look...

Andrea Del Bene replied on Wed, 2012/03/07 - 3:56am

Man, I faced the same problem and the same feeling just yesterday! Can't belive what a mess Hibernate has become!

Mason Mann replied on Wed, 2012/03/07 - 6:30am

The really good people have left Hibernate, and what's left is shit.

Lance Semmens replied on Thu, 2012/03/08 - 3:27am

Perhaps it's time to try MyBatis :)

Liam Knox replied on Mon, 2012/03/19 - 12:55pm in response to: Bruno Barin

The panacea of design.

Maybe change from sybase to db2 also :)

 

If only it was that simple 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.