Announcement Announcement Module
Collapse
No announcement yet.
sessionFactory or hibernateTemplate is required Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • sessionFactory or hibernateTemplate is required

    I am trying to create an application using HibernateTransactionManager. When deploying the applicationon Tomcat I get the following error.
    Code:
    2005-06-02 14:23:05 StandardContext[/rental]Loading Spring root WebApplicationContext
    2005-06-02 14:23:12 StandardContext[/rental]Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.chronos.inventory.dao.InventoryDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
    java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
    	at org.springframework.orm.hibernate3.support.HibernateDaoSupport.afterPropertiesSet(HibernateDaoSupport.java:116)
    I have looked over the applicationContext.xml file many times trying to find what I possibly could have screwed up with no luck. It appears that everything is created succesffully before this error. So it doesn't make sense why the sessionFactofy is unavailable. Maybe someone else could see what my problem is.

    applicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <!--
      - Application context definition on Hibernate.
    	-->
    <beans>
    
    	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
      
    	<!-- Configurer that replaces $&#123;...&#125; placeholders with values from a properties file -->
    	<!-- &#40;in this case, JDBC-related settings for the dataSource definition below&#41; -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location"><value>/WEB-INF/jdbc.properties</value></property>
    	</bean>
    
    	<!-- Local DataSource that works in any environment -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName"><value>$&#123;jdbc.driverClassName&#125;</value></property>
    		<property name="url"><value>$&#123;jdbc.url&#125;</value></property>
    		<property name="username"><value>$&#123;jdbc.username&#125;</value></property>
    		<property name="password"><value>$&#123;jdbc.password&#125;</value></property>
    	</bean>
    	<!--  JNDI DataSource that works in the container -->
    	<!-- bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                <value>java&#58;comp/env/jdbc/rental</value>
            </property>
        </bean-->
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    		<property name="mappingResources">
    			<list>
    				<value>com/chronos/mapping/Inventory.hbm.xml</value>
    				<value>com/chronos/mapping/Order.hbm.xml</value>
    				<value>com/chronos/mapping/Store.hbm.xml</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">$&#123;hibernate.dialect&#125;</prop>
    				<prop key="hibernate.show_sql">true</prop>
    			</props>
    		</property>
    	</bean>
    
    	<!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    
    	<!-- Transaction manager that delegates to JTA &#40;for a transactional JNDI DataSource&#41; -->
    	<!--
    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
    	-->
    
    
    	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
    
    	<!--
    		-
    		- Defines specific transaction attributes with "readOnly" markers,
    		- which is an optimization that is particularly valuable with Hibernate
    		- &#40;to suppress unnecessary flush attempts for read-only operations&#41;.
    		-
    		- Note that in a real-life app with multiple transaction proxies,
    		- we want to use parent and child bean definitions
    		- as described in the manual and as shown below, to reduce duplication.
        -->
    	<bean id="parentTransProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    		abstract="true">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    
    	
    
    	<!-- This bean inherits from the parentTransProxy bean -->
    	<bean id="inventory" parent="parentTransProxy">
    		<property name="target">
    			<bean class="com.chronos.inventory.dao.InventoryDao"/>
    		</property>
    	</bean>
    
      <!-- This bean inherits from the parentTransProxy bean -->
      <!-- bean id="store" parent="parentTransProxy">
        <property name="target">
          <bean class="com.chronos.store.dao.StoreDao"/>
        </property>
      </bean-->
    </beans>
    Thanks in advance!

  • #2
    I believe this points to the problem:
    java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
    at org.springframework.orm.hibernate3.support.Hiberna teDaoSupport.afterPropertiesSet(HibernateDaoSuppor t.java:116)
    Basically HibernateDaoSupport is throwing this exception because the sessionFactory or hibernateTemplate properties are not set. You basially need to change the definition of your inventory object as follows:

    Code:
    <!-- This bean inherits from the parentTransProxy bean -->
      <bean id="inventory" parent="parentTransProxy">
        <property name="target">
          <bean class="com.chronos.inventory.dao.InventoryDao">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
          </bean>
        </property>
      </bean>

    Comment


    • #3
      Perfect! That fixed it.

      My assumption was that because the parent bean uses the transactionManager and that in turn defines the sessionFactory property that it should be available to all children of that bean.

      Thanks for your help!

      Comment

      Working...
      X