Results 1 to 8 of 8

Thread: Lazy Load Problem when Doing UnitTest

  1. #1
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default 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 at 10:02 AM.

  2. #2
    Join Date
    Aug 2004
    Posts
    3

    Default

    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

  3. #3
    Join Date
    Aug 2004
    Posts
    3

    Default

    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]

  4. #4
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default

    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>

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    yoshi,

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

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default

    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.

  7. #7
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  8. #8
    Join Date
    Sep 2004
    Location
    China
    Posts
    17

    Default

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

    Bow
    Yoshi

Similar Threads

  1. Hibernate and lazy load
    By mfuller in forum Architecture
    Replies: 20
    Last Post: Apr 28th, 2009, 11:09 AM
  2. iBatis lazy load transaction issues.
    By efpiva in forum Data
    Replies: 3
    Last Post: Jun 22nd, 2005, 06:20 PM
  3. Hibernate and lazy load
    By mfuller in forum Data
    Replies: 2
    Last Post: Apr 15th, 2005, 12:37 PM
  4. Problem with Lazy fetching strategy
    By meissa in forum Data
    Replies: 1
    Last Post: Mar 21st, 2005, 11:52 AM
  5. Lazy load problem
    By frank huang in forum Data
    Replies: 3
    Last Post: Feb 1st, 2005, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •