View Full Version : application context
springuser
Aug 20th, 2004, 11:31 AM
I have a GenericHibernateDAOSupport class which has some reusable code which every xxxDAO can use.
Now, in my applicationcontext.xml file I want to define the sessionfactory for the GenericHibernateDAOSupport & not for every xxxDAO. How do I accomplish this? Any help is appreciated.
I have a defn,
<bean id="myxxxDAO" class="com.xxx.hibernate.xxxDAOHibernateImpl">
<property name="sessionFactory"><ref local="mySessionFactory"/></property>
</bean>
<bean id="myGenericDAO" class="com.xxx.dao.GenericHibernateDAOSupport">
<property name="sessionFactory"><ref local="mySessionFactory"/></property>
</bean>
Colin Sampaleanu
Aug 20th, 2004, 12:18 PM
One option is to make your DAOs inherit from the generic helper class. This is what Spring's HibernateDAOSupport class is for.
Then, you would normally have to set the session factory for each dao instance, but if you use child bean definitions, you can get away from this:
http://www.springframework.org/docs/reference/beans.html#beans-child-bean-definitions
Just make an abstract parent def which sets the sessionFactory, and the actual concrete DAOs would set that as the parent.
springuser
Aug 20th, 2004, 12:39 PM
Hi,
Thanks a lot for the input.
I tried using the child bean definitions as below but doesnt work:
gives an exception :
Caused by: java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
<!-- DAO object: Hibernate implementation -->
<bean id="myGenericDAO" class="com.xxx.dao.GenericHibernateDAOSupport">
<property name="sessionFactory"><ref local="mySessionFactory"/></property></bean>
<bean id="myDatadeliverabletrackingDAO" class="com.xxx.dao.hibernate.DatadeliverabletrackingDAOHi bernateImpl"
parent="myGenericDAO" >
</bean>
Colin Sampaleanu
Aug 20th, 2004, 12:59 PM
The parent bean def shouldn't have a class first of all, it's just an abstract definition. And it should be set to lazy-init so Sprig doesn't try to create it.
Where are yuo getting the error though?
Regards,
springuser
Aug 23rd, 2004, 12:20 PM
I am using junit to test my code. In my test class I get an handle of the bean "myDatadeliverabletrackingDAO" and then access the methods defined. I tried changing my code as mentioned, still doesnt work!!!
<!-- DAO object: Hibernate implementation -->
<bean id="myGenericDAO" lazy-init="true">
<property name="sessionFactory"><ref local="mySessionFactory"/></property></bean>
<bean id="myDatadeliverabletrackingDAO" class="com.xxx.dao.hibernate.DatadeliverabletrackingDAOHi bernateImpl"
parent="myGenericDAO" >
</bean>
Colin Sampaleanu
Aug 23rd, 2004, 12:49 PM
Child bean definitions got a lot more powerful as of 1.1RC1. Are you using 1.1RC1 or 1.1RC2?
Regards,
springuser
Aug 24th, 2004, 08:45 AM
That worked! I got spring 1.1RC2 and the child bean definitions worked.
Thanks a bunch.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.