Monday, April 20, 2009

Comparison of Hibernate with Netbeans and Eclipse

Netbeans 6.5.1 with Hibernate Support plugin 1.0.3

Update: Looks like the new Netbeans 6.7 has really improved Hibernate reverse-engineering support.

Netbeans’s Hibernate implementation is pleasantly simple, however its reverse-engineering is currently incomplete. If you want a straight-forward introduction to Java and Hibernate, Netbeans is the perfect way to go. But as soon as you want more powerful visual reverse-engineering tools, you’d best switch to Eclipse.

Steps to use Hibernate in Netbeans

  • Add Hibernate Support plugin
  • Setup a database connection in the Services window
  • Create a new Java Application project
  • Create a new Hibernate Configuration File (hibernate.cfg.xml)
    • Select the database connection
    • press Finish
  • Edit the hibernate.cfg.xml and add these properties
    hibernate.show_sql = true # under Configuration Properties (optional)
    hibernate.current_session_context_class = thread # under Miscellaneous Properties
  • Create new “Hibernate Mapping Files and POJOs from Database”
    • select the configuration file
    • choose the tables to reverse engineer
    • check “JDK 5 Language Features” to get type-safe generics
    • select a package where reverse engineered classes will go
    • press Finish
  • Create new “”
    • So Hibernate can find the configuration file even if it’s in the jar, I recommend changing the line
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();


      URL cfgurl = HibernateUtil.class.getResource("hibernate.cfg.xml");
      sessionFactory = new AnnotationConfiguration().configure(cfgurl).buildSessionFactory();

There’s currently no way to regenerate the reverse-engineered code once it’s been generated. This thread says it is “targeted as part of the next release” (Netbeans 6.7 in June '09).

Eclipse 3.4.2 with Hibernate Tools addon 3.2.4

Is it just me, or is it really hard to find up-to-date information on the web about setting up Hibernate in Eclipse? There is one tutorial that doesn’t seem too old, and covers most of my steps listed below.

Being new to Eclipse, it might be difficult to figure out there are actually 2 different addons you can download: Hibernate Tools, and Hibernate Synchronizer (last release 3 years ago?). There’s also MyEclipse that costs money but probably makes the whole process more seamless. To get started in Eclipse, it might be useful to understand how Eclipse does Java.

There are 4 different configurations to be aware of for Eclipse Hibernate Tools:

  1. The Configuration file (hibernate.cfg.xml), used by the Console Configuration (technically optional because most if not all of these settings can be made in the Console Configuration)

  2. The Console Configuration, used when creating the Reverse Engineering file, and by the Hibernate Code Generation configuration

  3. The Reverse Engineering file (hibernate.regeng.xml) used by the Hibernate Code Generation configuration

  4. The Hibernate Code Generation configuration

So with plain Eclipse, here’s how you setup Hibernate Tools.

  • Find the URL to the Eclipse Hibernate Tool addon
  • Add the site to the Software Updates and install the addon
  • Find all the libraries necessary to use Hibernate in your program and download them
  • Create a new Java Project
    • under the Libraries tab, add all those Hibernate libraries
  • Open up the project properties
    • in Hibernate Settings, check “Enable Hibernate 3 support”
  • Create a new Hibernate Configuration file (hibernate.cfg.xml)
    Note that this configuration file is only to be used by the Console Configuration
    • select database dialect
    • select driver class
    • enter the connection URL
    • check “Create a console configuration”
    • press Finish
  • Edit the hibernate.cfg.xml and add these properties
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>  <!-- optional -->
  • Create a new Hibernate Reverse Engineering file
    • select the Console Configuration you just made and press Refresh
    • select which tables to reverse engineer
  • Create a new Hibernate Code Generation configuration
    • select the Console Configuration
    • select the Output Directory (if the output directory contains your hibernate.cfg.xml, it will be overwritten by this task)
    • check “Reverse Engineer from JDBC Connection”
    • enter the regeng.xml file you created
    • under Exporters tab
      • check “Use Java 5 syntax” to get type-safe generics
      • check the following Exporters: Domain code, Hibernate XML Mappings, Hibernate XML Configuration
    • press Run
  • At this point your original Hibernate Configuration file could be replaced by the new one created by the Code Generation, or you can keep them separate
  • There’s no HibernateUtil generator, but all you really need is this
    URL cfgurl = this.getClass().getResource("hibernate.cfg.xml");
    SessionFactory sf = new AnnotationConfiguration().configure(cfgurl).buildSessionFactory();

In my specific case, I’m using Oracle 10g and the tables are in a schema named anotherschema. There are some things to watch out for.

  • Add the Oracle jdbc driver jar to your project libraries.
  • Case sensitivity!


  • Add the schema to the database connection properties after it’s created (this also means each schema needs a separate connection).
  • Has a problem that prevents me from reverse-engineering a database more than once without restarting the IDE (error says table “cannot be added because it does not have a primary key”).


  • Most places that need to read or refresh the database schema will often freeze Eclipse.


  1. Nice writeup, a few comments.

    "Eclipse has 4 required parts to be aware of for Hibernate Tools" - the only required part is the console configuration. The rest are purely optional.

    tip: Not setting default schema when reverse engineering oracle databases might be very slow

  2. Thanks Max. I've only been messing with Hibernate for a couple weeks so I'm still learning stuff.

    I eventually realized you could just configure all connection options in the Console Control, removing the need for the cfg.xml. This post also assumes people want to use the Reverse Engineering powers of Hibernate, which may not necessarily be the case. That's why I concluded Eclipse needs those 4 parts.

    Whenever I set the default schema with my database, Hibernate Tools would never see any of the tables. It was several hours of poking around before I removed that setting and finally all the tables were being listed. But you're right, it does take forever to load them all.

  3. Oracle metadata is case sensitive, if the schema is named SCOTT it should be SCOTT not scott ;)

  4. Thanks again Max! Tried specifying the default schema again, and now it works. I must have been using the wrong case before.

    The post has been updated with various corrections.