Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Maven dependencies to use hibernate, hibernate annotations, and ehcache?

I would like to update my Maven pom.xml with the latest hibernate, hibernate-annotations, and ehcache dependencies.

I read the hibernate download page and the ehcache donwload page. All my tries at interpreting it seem to fail. Can someone write out the snippet that should go into pom.xml?

Cryptic hibernate download page: http://hibernate.org/downloads

Cryptic ehcache download page: http://www.terracotta.org/dl/ehcache-oss-download-catalog

Here is what I have so far:

   <properties>
       <hibernate.version>3.6.0.Final</hibernate.version>
   </properties>
   <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
            </exclusion>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.2.0</version>
        <type>pom</type>
    </dependency>
 </dependencies>

    <repositories>
        <repository>
            <id>codehaus-repository</id>
            <name>Codehaus</name>
            <url>https://nexus.codehaus.org/content/repositories/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
        </repository>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-ehcache</id>
            <url>http://oss.sonatype.org/content/repositories/sourceforge-releases</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>JBoss Repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public</url>
            <layout>default</layout>
        </repository>
    </repositories>

I know, it looks like a big mess, because I don't seem to figure out, where all the dependencies are stored.

The above fails with:

Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

Update: Pascal, your reply is very clear and well explained. However, hibernate still fails on startup.

  1. sl4j threw errors, which went away by adding:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>
    
  2. There are many instances of this warning:

    g in the two-phase commit!
    1797 [main] WARN  net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory  - No Tr
    ansactionManagerLookup found in Hibernate config, XA Caches will be participatin
    
  3. Additionally, this error makes everything fail:

    Related cause:

    org.springframework.beans.factory.BeanCreationException: Error creating bean wit
    h name 'financialDAO' defined in ServletContext resource [/WEB-INF/config/applic
    ationContext-database.xml]: Cannot resolve reference to bean 'transactionManager
    ' while setting bean property 'transactionManager'; nested exception is org.spri
    ngframework.beans.factory.BeanCreationException: Error creating bean with name '
    transactionManager' defined in ServletContext resource [/WEB-INF/config/applicat
    ionContext-database.xml]: Cannot resolve reference to bean 'sessionFactory' whil
    e setting bean property 'sessionFactory'; nested exception is org.springframewor
    k.beans.factory.BeanCreationException: Error creating bean with name 'sessionFac
    tory' defined in ServletContext resource [/WEB-INF/config/applicationContext-dat
    abase.xml]: Invocation of init method failed; nested exception is java.lang.Null
    PointerException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver
    .resolveReference(BeanDefinitionValueResolver.java:328)
    

To confirm, this is the spring configuration:

<!-- Enable EHCache -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>

Update: This seems to be the latest exception that hibernate gets stuck on:

Caused by: java.lang.NullPointerException
    at net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:48)
    at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:79)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:236)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
like image 750
Thomas Avatar asked Nov 03 '10 23:11

Thomas


People also ask

What are the dependencies required to work with Hibernate?

System RequirementsHibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2. Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0. When building Hibernate 5.1 or older from sources, you need Java 1.7 due to a bug in the JDK 1.6 compiler.

How many dependencies should be added in POM XML for adding Hibernate into your application?

Add the two dependencies below into the dependencies tag of the pom. xml .

What are Hibernate Annotations?

Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.

Do I need Maven to work with Hibernate?

You do not need Maven to use Hibernate with Eclipse.


1 Answers

If you really mean the ultimate versions of hibernate-annotations (and not hibernate-entitymanager) and ehcache, then you'll need the following:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.0.Final</version>
    </dependency>
    <dependency>
       <groupId>net.sf.ehcache</groupId>
       <artifactId>ehcache-core</artifactId>
       <version>2.2.0</version>
   </dependency>
   ...
  </dependencies>

  <repositories>
    <repository>
      <id>repository.jboss.org-public</id>
      <name>JBoss repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
  </repositories>
  ...
</project>

As of Hibernate 3.6, JDK 1.4 support has been dropped and the Hibernate Annotations have been merged back into Core.

I you meant Hibernate EntityManager, then replace hibernate-core by hibernate-entitymanager in the above snippet.

Regarding EHCache, ehcache-core should provide everything you need (including implementations of the "new" Hibernate 3.3/3.5 caching SPI with its CacheRegionFactory). So, as documented:

Setting Ehcache as the Hibernate Second Level Cache

Hibernate 3.3 and higher

ATTENTION HIBERNATE 3.2 USERS

Make sure to note the change to BOTH the property name and value.

Use:

<property name="hibernate.cache.region.factory_class">
          net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

for instance creation, or

<property name="hibernate.cache.region.factory_class">
          net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>

to force Hibernate to use a singleton of Ehcache CacheManager.


Follow-up: To answer your update:

sl4j threw errors, which went away by adding (...)

Well, I can't guess what logging framework you're using, providing the binding of your choice was left as an exercise for the reader :)

There are many instances of this warning (...)

I get only one WARNING when running in a non JTA environment. If that's your case (a non JTA environment), I would consider the WARNING as normal. If that's not your case, have a look at the documentation about JTA.

Additionally, this error makes everything fail

This is somehow a different question (but it looks like the SessionFactory fails to initialize properly, activate logging to see why) and I suggest to post a new spring specific question.

like image 73
Pascal Thivent Avatar answered Sep 22 '22 05:09

Pascal Thivent