Announcement Announcement Module
Collapse
No announcement yet.
application context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • application context

    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>

  • #2
    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/...an-definitions

    Just make an abstract parent def which sets the sessionFactory, and the actual concrete DAOs would set that as the parent.

    Comment


    • #3
      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.Datadeliverabletracki ngDAOHibernateImpl"
      parent="myGenericDAO" >
      </bean>

      Comment


      • #4
        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,

        Comment


        • #5
          application context

          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.Datadeliverabletracki ngDAOHibernateImpl"
          parent="myGenericDAO" >
          </bean>

          Comment


          • #6
            Child bean definitions got a lot more powerful as of 1.1RC1. Are you using 1.1RC1 or 1.1RC2?

            Regards,

            Comment


            • #7
              application context

              That worked! I got spring 1.1RC2 and the child bean definitions worked.

              Thanks a bunch.

              Comment

              Working...
              X