Search code examples
hibernatespringormpojosessionfactory

ORM library jar into spring application to define a 'sessionFactory'


Problem

I can't define a correct sessionFactory bean in my project and fails web application initialization.

Log output

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webServiceTest' defined in ServletContext resource [/WEB-INF/
classes/appcontext-spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate
' is required
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 50 more
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
        at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
        at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)
        ... 60 more

I've created an ORM library with maven using xml mappings and pojos. I've imported into my project and I can't configure to access into DDBB.

  • jdbc.properties file is well defined with DDBB connection (db.url, db.properties, etc).
  • All POJOs in library extend HibernateDaoSupport and implement basic op interface (CRUD). I'm using a service webServiceTest that extends DaoBase to make a test.
  • I've did JUnit tests in ORM-library with good results.

Here is dataSource, sessionFactory, etc definitions from appcontext-persistance.xml file:

    <!-- PERSISTENCE SETUP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
        <value>${db.driver}</value>
    </property>
    <property name="url">
        <value>${db.url}</value>
    </property>
    <property name="username">
        <value>${db.username}</value>
    </property>
    <property name="password">
        <value>${db.password}</value>
    </property>

    <property name="maxActive">
        <value>${db.maxActive}</value>
    </property>

    <!-- Maximum number of idle dB connections to retain in pool. Set 
        to 0 for no limit -->
    <property name="maxIdle">
        <value>${db.maxIdle}</value>
    </property>

    <!-- Maximum time to wait for a dB connection to become available 
        in ms, in this example 10 seconds. An Exception is thrown if this timeout 
        is exceeded. Set to -1 to wait indefinitely. -->
    <property name="maxWait">
        <value>${db.maxWait}</value>
    </property>

    <!-- Autocommit setting. This setting is required to make Hibernate 
        work. Or you can remove calls to commit(). -->
    <property name="defaultAutoCommit">
        <value>true</value>
    </property>

    <!-- Recover abandoned connections -->
    <property name="removeAbandoned">
        <value>true</value>
    </property>

    <!-- Set the number of seconds a dB connection has been idle before 
        it is considered abandoned. -->
    <property name="removeAbandonedTimeout">
        <value>11</value>
    </property>

    <!-- Log a stack trace of the code which abandoned the dB connection 
        resources. -->
    <property name="logAbandoned">
        <value>true</value>
    </property>

</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="lobHandler">
        <ref bean="oracleLobHandler" />
    </property>
    <property name="dataSource" ref="dataSource" />

    <!-- configura la fuente de los mappings -->
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />

    <property name="hibernateProperties">
        <props>

            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
            <prop key="hibernate.generate_statistics">true</prop>

        </props>
    </property>
</bean>

Questions:

  • Spring configuration has to load hibernate.cfg.xml from ORM jar? How can I do this ?
  • How should I load a configuration from a Spring ORM project into another ?
  • I define the mappings in ORM but I don't have any application context defined in ORM to include in the jar.

Solution

  • I've solve this issue using the attribute for beans tag default-autowire="byName" and it loads all configurations for object injections needed in appcontext-spring.xml.

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
        default-lazy-init="true" default-autowire="byName">
    

    And creates the sessionFactory.