开发者

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(AbstractAutowireCapableBeanFact开发者_Go百科ory.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>


a)

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

Source:

  • Spring 3.1.x Reference > Hibernate

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


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


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.


Here's a simple file that worked for me, I used transaction manager and not template.

<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/stackoverflow" />
            <property name="user" value="root" />
            <property name="password" value="******" />
            <property name="maxPoolSize" value="50" />
    </bean>

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="mySqlDataSource" />
            <property name="mappingResources">
                    <list>
                            <value>Post.hbm.xml</value>
                            <value>Tag.hbm.xml</value>
                            <value>User.hbm.xml</value>
                    </list>
            </property>

            <property name="hibernateProperties">
                    <value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                    </value>
                    <!--
                            hibernate.connection.provider_class =
                            org.hibernate.connection.C3P0ConnectionProvider
                            hibernate.hbm2ddl.auto=update
                            hibernate.current_session_context_class=thread
                    -->
            </property>
    </bean>

    <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
            <!--
                    <property name="dataSource" ref="mySqlDataSource"/>
            -->
    </bean>

Also, I think the hibernate download package comes with separate folders that indicate what its specific dependencies are. As for the interrelation between hibernate and spring, I think you will have to use some dependency management tool as others have suggested. I do not think maven is a convenient tool. Just don't ever depart from maven's expected project structure and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜