Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct version of Spring and Hibernate and required dependencies...Exception due to dependencies

Since three days i am trying to get my Spring-Hibernate programs run. i had really tough time finding the involved dependencies due to version difference between hibernate2 and hibernate3. Finally i was able to run program with following set of dependencies

  • cglib-nodep-2.1_3.jar
  • commons-collections.jar
  • commons-dbcp.jar
  • commons-pool.jar
  • commons-logging.jar
  • dom4j-1.4.jar
  • ehcache-1.6.0-beta1.jar
  • hibernate-3.1.3.jar
  • hsqldb.jar
  • jta.jar log4j-1.2.9.jar
  • mysql-connector-java-5.0.8-bin.jar
  • org.springframework.orm-3.1.0.M1.jar
  • org.springframework.transaction-3.1.0.M1.jar
  • spring-2.5.6.jar
  • spring-beans-2.0.4.jar

now after two days of efforts when i was able to manage the above mentioned dependencies i tried building similar program but it is throwing following error.I tried online for solution but the solution which i found there is not correct version of spring and hibernate ...Can any one tell me correct cause of exception and also correct version of the Spring and hibernate


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRecordDAO' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)

I am also adding my application context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/subhash"/>
        <property name="username" value="root"/>
        <property name="password" value=""></property>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <value>MyRecord.hbm.xml</value>
            </list>
         </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>

    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="myRecordDAO" class="com.shoesobjects.MyRecordDAOHibernateWithSpring">
        <property name="hibernateTemplate"><ref local="hibernateTemplate"/></property>
    </bean>
</beans>
like image 471
Anupam Gupta Avatar asked Mar 16 '11 13:03

Anupam Gupta


People also ask

Which version of hibernate is compatible with spring 5?

Spring 5x is compatible with hibernate 4x unless you are using it as an implementation of JPA which might not be compatible. A suggestion would be using the latest hibernate.

What are the dependencies required for spring?

Basic Spring Dependencies With Maven For example, the basic Spring Context can be without the Persistence or the MVC Spring libraries. This dependency – spring-context – defines the actual Spring Injection Container and has a small number of dependencies: spring-core, spring-expression, spring-aop, and spring-beans.

Which spring bean is required to configure hibernate with spring?

Below spring bean configuration file will work for Spring 4 and Hibernate 4 versions. For hibernate 4, we need to use org. springframework. orm.

What is the current spring boot version?

What is the latest Spring Boot version? The current stable version, as of July 2022, is Spring Boot 2.7.


3 Answers

a)

Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.

Source:

b)

  • org.springframework.orm-3.1.0.M1.jar
  • org.springframework.transaction-3.1.0.M1.jar
  • spring-2.5.6.jar
  • spring-beans-2.0.4.jar

Do you really think mixing current pre-release versions (3.1.x) with ancient versions (2.0.4 was released in 2007) is a good idea?


As matt says: use a dependency managements system like Maven, what you are dealing with is jar hell. Here's an article about referencing Spring 3.x artifacts from maven:

Obtaining Spring 3 Artifacts with Maven

like image 70
Sean Patrick Floyd Avatar answered Sep 22 '22 14:09

Sean Patrick Floyd


I suggest using a dependency management tool like Maven or Apache Ivy so you don't have to handle sorting through the dependencies and required versions on your own.

  • Hibernate docs on using with Maven
like image 29
matt b Avatar answered Sep 23 '22 14:09

matt b


Got my problem resolved ...As pointed by Sean it was due to mixing different versions of spring and hibernate. Message for other newbies is please use latest version of spring and hibernate.As of Spring 3.0, Spring requires Hibernate 3.2 or later.one should never mix old and new version.

like image 26
Anupam Gupta Avatar answered Sep 23 '22 14:09

Anupam Gupta