Announcement Announcement Module
Collapse
No announcement yet.
Lazy Load Problem when Doing UnitTest Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Load Problem when Doing UnitTest

    Hello all

    I came cross the lazy load problem when using junit to test my program.
    I searched the forum and found the solution(Here is the link to that post:
    (http://forum.springframework.org/showthread.php?t=9630). But as soon as I overcame that problem follow by the solution in that post, unfortunely I face another problem. my save operation seem not to be committed.

    BTW:I use dbunit to test my database related code.

    The following is the code snippet from my program:

    1.The UnitTest code snippet

    Code:
        protected void setUp() throws Exception
        {
            super.setUp();
    
            emptyAllTables();
            entity = new Entity();
            entity.setName("Test");
    /*
            sessionFactory = (SessionFactory) BeanLocator.getBean("mySessionFactory");
            Session session = SessionFactoryUtils.getSession(sessionFactory, true);
            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    */
        }
    
        protected void tearDown() throws Exception
        {
    /*
            SessionHolder sessionHolder =
                    (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
            SessionFactoryUtils.closeSessionIfNecessary(sessionHolder.getSession(), sessionFactory);
    */
    
            super.tearDown();
        }
    if I uncomment the commentted code, every thing goes well except the find method!And if I comment the code the test will come across the Lazy Load problem.

    Code:
        public void testSave() throws SQLException, DataSetException
        {
            ITable table = connection.createQueryTable("ExceptedData", QUERY);
            assertEquals(0, table.getRowCount());
    
            baseDAO.save(entity);
            table = connection.createQueryTable("ExceptedData", QUERY);
            assertEquals(1, table.getRowCount());
        }
    public void testFind()
    {
    //.....
    }

    2.the save method

    Code:
        public Object save(Object object)
        {
            return getHibernateTemplate().save(object);
        }
    i am sure that the transaction haven't been committed, because i can't see any insertion info from the log file!
    where is the problem? how can I resolve this problem! Can anyone give me a hand?

    thanx in advance for any suggestion!

    Regards
    Yoshiyan
    Last edited by robyn; May 14th, 2006, 11:02 AM.

  • #2
    try

    Code:
       public void testSave() throws SQLException, DataSetException
        {
            ITable table = connection.createQueryTable("ExceptedData", QUERY);
            assertEquals(0, table.getRowCount());
    
            baseDAO.save(entity);
    
    // begin SWAG
    SessionHolder holder = TransactionSynchronizationManager.getResource(sessionFactory); 
    holder.getTransaction().commit();
    // end SWAG
    
            table = connection.createQueryTable("ExceptedData", QUERY);
            assertEquals(1, table.getRowCount());
        }
    hope that works for you,
    -wd

    Comment


    • #3
      Just as an after thought to my previous post, you may need to call session.setFlushMode(FlushMode.COMMIT) in the setup. Once again, I don't know for sure, but thought I'd mention it.

      -wd[/code]

      Comment


      • #4
        wdemoss, thank you for you reply 8) , I have try your method.unfortunately it doesn't work! It comes across a NullPointException.

        // begin SWAG
        SessionHolder holder = TransactionSynchronizationManager.getResource(sess ionFactory);
        holder.getTransaction().commit();
        // end SWAG
        the holder.getTransaction return a NULL value.

        Here is the snippet from my config file. Is there any problem in my config file? Once again, if i uncomment the commentted code , every thing goes well except for the find method that refers the lazy load.
        Code:
            <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
                <property name="mappingResources">
                    <list>
                        <value>gameshop/framework/testmodel/Entity.hbm.xml</value>
                        <value>gameshop/framework/testmodel/EntityChild.hbm.xml</value>
                    </list>
                </property>
            </bean>
        
            <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
                <property name="sessionFactory"><ref local="mySessionFactory"/></property>
            </bean>
        
            <bean id="baseDAO" class="gameshop.framework.dao.impl.BaseDAOHibernateImpl">
                <property name="sessionFactory"><ref local="mySessionFactory"/></property>
            </bean>

        Comment


        • #5
          yoshi,

          is myTransactionManager bound to baseDAO? could you provide the confguration that shows this relation?

          Comment


          • #6
            IMO, the myTransactionManager has been bound to baseDAO,because BaseDAOHibernateImpl is a subclass of HibernateDaoSupport.

            Code:
            public class BaseDAOHibernateImpl extends HibernateDaoSupport implements IBaseDAO
            &#123;
                public Object save&#40;Object object&#41;
                &#123;
                    return getHibernateTemplate&#40;&#41;.save&#40;object&#41;;
                &#125;
            
                //omit the others...........
            &#125;
            and I have the flowing configuration in my cofig file

            Code:
                <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
                    <property name="mappingResources">
                        <list>
                            <value>gameshop/framework/testmodel/Entity.hbm.xml</value>
                            <value>gameshop/framework/testmodel/EntityChild.hbm.xml</value>
                        </list>
                    </property>
                </bean>
            
                <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
                    <property name="sessionFactory"><ref local="mySessionFactory"/></property>
                </bean>
            
                <bean id="baseDAO" class="gameshop.framework.dao.impl.BaseDAOHibernateImpl">
                    <property name="sessionFactory"><ref local="mySessionFactory"/></property>
                </bean>
            Becase I extend from HibernateDaoSupport and config the sessionFactory for the baseDAO,so i think that the myTransactionManager has been bound to baseDAO,spring will do for it. Is my thinking right?

            There is no other position has the relation with myTransactionManager?Have I miss any things?

            Thank you for your kind help.

            Comment


            • #7
              Becase I extend from HibernateDaoSupport and config the sessionFactory for the baseDAO,so i think that the myTransactionManager has been bound to baseDAO,spring will do for it. Is my thinking right?
              Well, you need to specify this explicitely and tell Spring what method in your DAO should be wrapped in transactions. Following is an example of applying transaction management to your dao:
              Code:
                <bean id="myDAO"
                      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
                      abstract="true">
                  <property name="target">
                    <ref local="baseDAO"/>
                  </property>
                  <property name="transactionManager">
                    <ref local="transactionManager"/>
                  </property>
                  <property name="transactionAttributes">
                    <props>
                      <prop key="*">PROPAGATION_REQUIRED</prop>
                    </props>
                  </property>
                </bean>
              HTH

              Comment


              • #8
                It works well now ,thank you for your help,Kind men. m~_~m

                Bow
                Yoshi

                Comment

                Working...
                X