Help

Quite a few folks have asked about the new Hibernate Core modules introduced with the 3.3 release: what each is for, when to use which, etc. I wanted to take a bit to describe what each of the main modules is for and when/how you would use certain combinations. (These modules also define maven artifacts; all Hibernate Core artifacts use org.hibernate groupId).

The modules

  • hibernate-core - defines the basic functionality of Hibernate. It is expected that this module will be split up further in future releases.
  • hibernate-jmx - defines the functionality for Hibernate to integrate with JMX servers. Both the org.hibernate.jmx.HibernateService and org.hibernate.jmx.StatisticsService are defined in this module.
  • hibernate-ehcache - defines the integration for using EhCache as Hibernate's second level cache
  • hibernate-jbosscache - defines the integration for using JBossCache as Hibernate's second level cache, based on the 1.x versions of JBossCache. This integration should be considered deprecated in favor of the newer 2.x based integration...
  • hibernate-jbosscache2 - defines the integration for using JBossCache as Hibernate's second level cache, based on the 2.x versions of JBossCache
  • hibernate-oscache - defines the integration for using OSCache as Hibernate's second level cache
  • hibernate-swarmcache - defines the integration for using SwarmCache as Hibernate's second level cache
  • hibernate-c3p0 - defines the integration for using the c3p0 connection pool as Hibernate's ConnectionProvider
  • hibernate-proxool - defines the integration for using the proxool connection pool as Hibernate's ConnectionProvider
  • hibernate-parent - this acts as the parent pom for all the Hibernate modules. Users will generally never use nor need to know about this module. I wanted to mention it here simply because it might be useful for folks developing extensions to Hibernate.
  • hibernate-testing - defines the basic infastructure used by the Hibernate testsuite for building SessionFactories and managing Sessions. Was broken out as a separate module because I thought users might find it useful as well for testing Hibernate-related code.

There are other modules, but the above ones are the ones developers will usually need to use (the others include things like DocBook sources).

Using

The minimum dependency set-up with Javassist and using log4j as logging library would be:

<dependencies>
    <!-- the dependency on the hibernate-core module (see above) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <!-- want to use Javassist as Hibernate's bytecode provider, so we depend on it here. -->
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.4.GA</version>
    </dependency>
    <!-- Logging set up -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>

To use cglib instead of Javassist, we would have this instead:

<dependencies>
    <!-- the dependency on the hibernate-core module (see above) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <!-- Here we are using cglib instead of Javassist (Hibernate 'repackages' cglib to avoid name clashes) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-cglib-repack</artifactId>
        <version>2.1_3</version>
    </dependency>
    <!-- Logging set up -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>

Now, lets add support for second level caching with EhCache:

<dependencies>
    <!-- the dependency on the hibernate-core module (see above) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <!-- Here we are using cglib instead of Javassist (Hibernate 'repackages' cglib to avoid name clashes) -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-cglib-repack</artifactId>
        <version>2.1_3</version>
    </dependency>
    <!-- Logging set up -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <!-- Here is the bit enabling EhCache support -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
</dependencies>

As we stated above, we said that the hibernate-ehcache module defines the integration with EhCache. It also defines a dependency on EhCache itself, which maven pulls into our dependencies here transitively. See the maven documentation for information about how this all works. The point is simply that we just need to depend on the hibernate-ehcache module and the other deps need to use EhCache are handled for us.

11 comments:
 
12. Sep 2008, 21:31 CET | Link
Dave

log4j is a dependency of slf4j-log4j12, so you can omit the following:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
ReplyQuote
 
14. Sep 2008, 01:14 CET | Link

Oops ... so you're saying that if I have a dependency on hibernate-core and c3p0 but not hibernate-c3p0 I'm not using a connection pool? Does hibernate-core have a built-in connection pool?

 
15. Sep 2008, 02:51 CET | Link

Version overridding aside, yes you are correct. Not sure which log4j version they pull in.

 
15. Sep 2008, 02:59 CET | Link

Correct. To use hibernate and c3p0 you'd use:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.4.GA</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.2</version>
    </dependency>
    <!-- omit log4j, per Dave above -->
</dependencies>

You could define a dep directly on c3p0 as well, but you need to depend on hibernate-c3p0. If your code directly uses c3p0 in any fashion, then it would in fact be correct to directly depend on it too.

 
16. Sep 2008, 02:37 CET | Link
Am I correct in assuming that this configuration goes in your hibernate.cfg.xbm? Where does this go?

Thanks,
Lincoln
 
04. Oct 2008, 04:37 CET | Link
Christian
Even if you add hibernate-c3p0 in your pom you still need to set the "hibernate.connection.provider_class" property in your hibernate.cfg.xml to this value:

org.hibernate.connection.C3P0ConnectionProvider

Otherwise the build-in connection pool will still be used!

This behavior started appearing with 3.3.0...

Christian
 
14. Oct 2008, 15:37 CET | Link
Oleg | edge(AT)ua.fm

I've tried to use repacked cglib as bytecode provider, so I changed javassist dependency to: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-cglib-repack</artifactId> <version>2.1_3</version> </dependency> But I still get java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter exception. Are there any changes to be made to Hibernate config as well, so it does actually swaps javassist with cglib?

 
25. Jan 2009, 21:40 CET | Link

I have the same problem as well and it prevents me to actually migrate from 2.5.6 which has a regression with JTA provider lookups. Can you clarify the role of javassist vs. cglib? It seems to me that it is impossible to run Hibernate with only cglib anymore. Very very annoying.

Also, please note that the sample in your blog post regarding cglib just does not work. Create an entity in a project with those dependencies and it will blow up on startup with the NoClassDefFoundError on MethodFilter.

 
18. Nov 2009, 00:16 CET | Link
Gui
It seems that starting from hibernate-core 3.3.2, the repackaged version of cglib is not needed anymore. Instead, use the plain cglib:

    <dependency>
         <groupId>cglib</groupId>
         <artifactId>cglib</artifactId>
         <version>2.2</version>
    </dependency>

Also, the hibernate.bytecode.provider=cglib property MUST be specifed in a hibernate.properties (or as system property). It's ignored when specified eg in a spring config file.

PS: Although an old entry, it shows up on top in google...
 
05. Oct 2014, 09:37 CET | Link
lkmjn

Doing this will allow audience to see how much effort was put into maintaining the residence while you owned it. Sell your property fast

Post Comment