DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Hibernate – Please Don't Deprecate Yourself

Hibernate – Please Don't Deprecate Yourself

Rob Gordon user avatar by
Rob Gordon
·
Mar. 07, 12 · Java Zone · Interview
Like (0)
Save
Tweet
11.54K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • API Security Weekly: Issue 165
  • How Low Code Demands More Creativity From Developers
  • SQL vs. NoSQL: Pros and Cons
  • How To Evaluate Software Quality Assurance Success: KPIs, SLAs, Release Cycles, and Costs

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo