facebook

org.hibernate.LazyInitializationException

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #253095 Reply

    schanamolu
    Member

    Hi,

    Iam using spring automatic handle session and create/close session. How do I use spring transactional session (will not be closed) .

    Iam posting spring application-context.xml file

    
         <bean id="userTransService" class="com.cvg.ap.ws.model.service.impl.UserServiceImpl">
        
            <property name="userDAO"><ref local="userDAO"/></property>
            <property name="companyDAO"><ref local="companyDAO"/></property>
             <property name="companyAppDAO"><ref local="companyAppDAO"/></property>
             <property name="apgroupmembersDAO"><ref local="apgroupmembersDAO"/></property>
             <property name="acmgroupservicesDAO"><ref local="acmgroupservicesDAO"/></property>
            <property name="acmservicesDAO"><ref local="acmservicesDAO"/></property>
            <property name="applicationDAO"><ref local="applicationDAO"/></property>
            
        </bean>
        
        <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager"><ref local="transactionManager"/></property>
            <property name="target"><ref local="userTransService"/></property>
            <property name="transactionAttributes">
                <props>
                    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    

    Here is full sourceCode for your reference

    
    
        public List GetACMServicesForUser(String userId, String apType )
        {
            List xmap = new ArrayList();
            Users user = this.userDAO.getUser(userId);
            List groupmemberList = apgroupmembersDAO.findGroupsByUserId(user);
            
            List groupServicesList = null;
            
            List companyList = null;
            
            List finalAcmServicesList = new ArrayList();
            List finalApplicationsList = new ArrayList();
            List finalCompanyList = new ArrayList();
            
            
            String x = null;
                    
            for (Iterator it = groupmemberList.iterator(); it.hasNext(); ){
                
                Groupmember groupmember = (Groupmember) it.next();
                Groups apgroups  = groupmember.getId().getGroups();
                
                
                groupServicesList = acmgroupservicesDAO.findACMGroupServicesByGroupId(apgroups);
                
        //        for (Iterator grpSvcItr = groupServicesList.iterator();  grpSvcItr.hasNext(); ){
                
                   for (int k = 0; k < groupServicesList.size(); k++){
                       
                       Acmgroupservice acmgroupservice = (Acmgroupservice)groupServicesList.get(k);
                    
       //             Acmgroupservice acmgroupservice = (Acmgroupservice) grpSvcItr.next();
                    Acmservice acmservice = acmservicesDAO.get(acmgroupservice.getId().getAcmservice().getAcmserviceid());
                    
                    if (!finalAcmServicesList.contains(acmservice))
                        finalAcmServicesList.add(acmservice);
                    
                   }
                
                List arrayList = new ArrayList();
                    
                for (Iterator SvcItr = finalAcmServicesList.iterator(); SvcItr.hasNext(); ){
                
                    Acmservice acmservice  = (Acmservice)SvcItr.next();
                    String appName = acmservice.getApplication().getAppname();
                    ApplicationVO appVO =  GetApplicationVO(finalAcmServicesList, appName); 
                    finalApplicationsList.add(appVO);
            }                
        }
            
            for (Iterator appIter = finalApplicationsList.iterator();  appIter.hasNext(); ){
            
                ApplicationVO tempAppVO = (ApplicationVO) appIter.next();
                companyList = applicationDAO.findByCompanyId(tempAppVO.getCompanyId());
                Company company = (Company) companyList.get(0);
                String companyName = company.getCompanyname();
                CompanyVO tempVO =  GetCompanyVO(finalApplicationsList,companyName);
                finalCompanyList.add(tempVO);
            } 
            return finalCompanyList;  
        }
        
        ApplicationVO GetApplicationVO (List finalAcmServicesList, String name ){
            
            List arrayList = new ArrayList();
            Acmservice acmservice = null;
            
            for (Iterator SvcItr = finalAcmServicesList.iterator(); SvcItr.hasNext(); ){    
                acmservice  = (Acmservice)SvcItr.next();
                String appName = acmservice.getApplication().getAppname();
                if (appName.compareTo(name)==0){
                    ACMServiceVO acmServiceVO = GetACMServiceVO(acmservice);
                    arrayList.add(acmServiceVO);
                }
            } 
            
            applicationVO.setApplicationName(name);
            applicationVO.setAppid(acmservice.getApplication().getId().getAppid());
            applicationVO.setCompanyId(acmservice.getApplication().getId().getCompany().getCompanyid());    
              applicationVO.setServices(arrayList);
                
                return applicationVO;
        }
        
    
        CompanyVO GetCompanyVO (List finalApplicationList, String name ){
            
            List arrayList = new ArrayList();
              ApplicationVO appVO = null;
            Company company = null;
            
            for (Iterator appItr = finalApplicationList.iterator(); appItr.hasNext(); ){
                
                appVO  = (ApplicationVO)appItr.next();
                 List companyList = applicationDAO.findByCompanyId(appVO.getCompanyId());
                 company = (Company) companyList.get(0);
                String companyName = company.getCompanyname();
                    
                if (companyName.compareTo(name)==0)
                    arrayList.add(appVO);
            }
            
              companyVO.setCompanyID(company.getCompanyid());
            companyVO.setCompanyName(company.getCompanyname());
            companyVO.setApplications(arrayList);
            
            return companyVO;
        }
        
        
        ACMServiceVO GetACMServiceVO(Acmservice acmservice){
            
            ACMServiceVO tempVO = new ACMServiceVO();
            tempVO.setServiceid(acmservice.getAcmserviceid());
            tempVO.setContentsource(acmservice.getContentsource());
            tempVO.setContentdirectory(acmservice.getContentdirectory());
            tempVO.setServicedescription(acmservice.getServicedescription());
            tempVO.setApid(acmservice.getApplication().getId().getAppid());
            tempVO.setCompanyid(acmservice.getApplication().getId().getCompany().getCompanyid());
            tempVO.setAptype(acmservice.getAptype());
            return tempVO;
        }
        
        
        UsersVO GetUsersVO(Users users){
            
            UsersVO tempVO = new UsersVO();
            
            tempVO.setUserid(users.getUserid());
            tempVO.setPassword(users.getPassword());
            tempVO.setUsername(users.getUsername());
            tempVO.setCreateddate(users.getCreateddate());
            tempVO.setLastmodified(users.getLastmodified());
            tempVO.setLastmodifiedby(users.getLastmodifiedby());
            tempVO.setCreatedby(users.getCreatedby());
            tempVO.setPasswordflag("true");
            return tempVO;
        
        }
    
    }
    
     
    

    The following Piece of code of the above function is throwing exception.

    
     
                   if (!finalAcmServicesList.contains(acmservice))
                        finalAcmServicesList.add(acmservice);
                    
                   }
                
                List arrayList = new ArrayList();
                    
                for (Iterator SvcItr = finalAcmServicesList.iterator(); SvcItr.hasNext(); ){
                
                    Acmservice acmservice  = (Acmservice)SvcItr.next();
                    String appName = acmservice.getApplication().getAppname();
                    ApplicationVO appVO =  GetApplicationVO(finalAcmServicesList, appName); 
                    finalApplicationsList.add(appVO);
            }                
    
    
    
    #253096 Reply

    schanamolu
    Member

    Hi,

    I did make a change to application-context.xml

    <beans default-autowire=”no” default-lazy-init=”false” default-dependency-check=”none”>

    I also make a change to Application-context.hbm.xml

    <?xml version=”1.0″?>
    <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”&gt;
    <!–
    Mapping file autogenerated by MyEclipse – Hibernate Tools
    –>
    <hibernate-mapping>
    <class name=”com.cvg.ap.ws.model.businessobject.Application” table=”APPLICATION” schema=”ACCESSPOINT3″>
    <composite-id name=”id” class=”com.cvg.ap.ws.model.businessobject.ApplicationId”>
    <key-property name=”appid” type=”java.lang.String”>
    <column name=”APPID” length=”20″ />
    </key-property>
    <key-many-to-one name=”company” class=”com.cvg.ap.ws.model.businessobject.Company”>
    <column name=”COMPANYID” length=”20″ />
    </key-many-to-one>
    </composite-id>
    <property name=”appname” type=”java.lang.String”>
    <column name=”APPNAME” length=”20″ not-null=”true” />
    </property>
    <set name=”acmservices” lazy=”false” inverse=”true”>
    <key>
    <column name=”APID” length=”20″ />
    <column name=”COMPANYID” length=”20″ not-null=”true” />
    </key>
    <one-to-many class=”com.cvg.ap.ws.model.businessobject.Acmservice” />
    </set>
    </class>
    </hibernate-mapping>

    Still Iam getting the exception.

    Let me know..

    #253097 Reply

    Haris Peco
    Member

    Hi,

    see this about spring transaction
    http://www.springframework.org/docs/reference/transaction.html

    you can try next , too

    your code :

    Acmservice acmservice = acmservicesDAO.get(acmgroupservice.getId().getAcmservice().getAcmserviceid());
                  
                  if (!finalAcmServicesList.contains(acmservice))
                     finalAcmServicesList.add(acmservice);
                  
                  }
     

    populate all property in acmservicesDAO.get(…) , like getApplication etc
    or
    make acmservice non-lazy
    or call complete your function in spring callback

    Best

    #253099 Reply

    schanamolu
    Member

    Hi,

    I did make a change to application-context.xml

    <beans default-autowire=”no” default-lazy-init=”false” default-dependency-check=”none”>

    I also make a change to Application.hbm.xml & acmservice.hbm.xml. Iam still geting the exception. Can you please specific How can I set transactions in spring. Please look at my application-context.xml in previous posting.

    
    
    <?xml version="1.0"?> 
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    <!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools 
    --> 
    <hibernate-mapping> 
    <class name="com.cvg.ap.ws.model.businessobject.Application" table="APPLICATION" schema="ACCESSPOINT3"> 
    <composite-id name="id" class="com.cvg.ap.ws.model.businessobject.ApplicationId"> 
    <key-property name="appid" type="java.lang.String"> 
    <column name="APPID" length="20" /> 
    </key-property> 
    <key-many-to-one name="company" class="com.cvg.ap.ws.model.businessobject.Company"> 
    <column name="COMPANYID" length="20" /> 
    </key-many-to-one> 
    </composite-id> 
    <property name="appname" type="java.lang.String"> 
    <column name="APPNAME" length="20" not-null="true" /> 
    </property> 
    <set name="acmservices" lazy="false" inverse="true"> 
    <key> 
    <column name="APID" length="20" /> 
    <column name="COMPANYID" length="20" not-null="true" /> 
    </key> 
    <one-to-many class="com.cvg.ap.ws.model.businessobject.Acmservice" /> 
    </set> 
    </class> 
    </hibernate-mapping> 
     
    

    Please also review the code

    
     
    /**
         * Get Acmservice by acmserviceid
         *
         * @param acmserviceid
         * @return
         */
        public Acmservice get(String acmserviceid)
        {
            return (Acmservice) retrieve(Acmservice.class, acmserviceid);
        }
    
    
    /**
         * Retrieves and <code>Object</code> of the class type specified
         * by <code>c</code>, and having the given <code>id</code>.
         *
         * @param entityClass
         * @param id
         * @return
         * @throws PersistenceException
         */
        protected Object retrieve(Class entityClass, Serializable id)
        {
            return getHibernateTemplate().get(entityClass, id);
        }
    
    Please be specific how can I populate when iam unable to get some foreign key values from database.
    
    Thanks,
    Sreedhar
        
    
    #253104 Reply

    Haris Peco
    Member

    Sreedhar ,

    Spring lazy loading (applicationContext.xml) isn’t related with hibernate loading.It load/no load bean on creating spring context and it isn’t related with your case.

    You have to explore transaction management from spring if you want set correct spring and hibernate lazy loading, but just try you can set next :

    SessionFactory sessionFactory = (SessionFactory) yourSpringContext.getBean(“yourLocalSessionFactoryBean”);
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    TransactionSynchronizationManager.bindResource(sessionFactory,
    new SessionHolder(session));

    …. call your function
    …. all DAO will use same session here

    TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.releaseSession(session, sessionFactory);

    Best

    Best

    #253106 Reply

    schanamolu
    Member

    Hi,

    Lazy exception is resolved with the help of following Link.

    http://www.jroller.com/page/kbaum?entry=orm_lazy_initialization_with_dao

    http://forum.springframework.org/showthread.php?t=25096&highlight=OpenSessionInViewInterceptor

    Key point is we should use hibernateInterceptor which helped me to resolve the issue.

    
        <bean id="userTransService" class="com.cvg.ap.ws.model.service.impl.UserServiceImpl">
        
            <property name="userDAO"><ref local="userDAO"/></property>
            <property name="companyDAO"><ref local="companyDAO"/></property>
             <property name="companyAppDAO"><ref local="companyAppDAO"/></property>
             <property name="apgroupmembersDAO"><ref local="apgroupmembersDAO"/></property>
             <property name="acmgroupservicesDAO"><ref local="acmgroupservicesDAO"/></property>
            <property name="acmservicesDAO"><ref local="acmservicesDAO"/></property>
            <property name="applicationDAO"><ref local="applicationDAO"/></property>
            
        </bean>
        
    
        <bean id="userInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager"><ref local="transactionManager"/></property>
            <property name="transactionAttributes">
                <props>
                    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
                
        </bean>
    
    
        <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
             <property name="sessionFactory">
               <ref bean="sessionFactory"/>
             </property>
        </bean>
    
    
    
        <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
          <property name="interceptorNames">
          <list>
               <value>hibernateInterceptor</value>
             <value>userInterceptor</value>
            </list>
           </property>
         <property name="target">
             <ref local="userTransService"/>
        </property>
        </bean> 
    

    Good Luck guys.

    Thanks,
    Sreedhar

    #253107 Reply

    schanamolu
    Member

    Being Lazy in the Business Layer

    Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward.

    Thanks again

    #253983 Reply

    panickert
    Member

    This is an excellent find; outstanding post.

    Thanks

Viewing 8 posts - 16 through 23 (of 23 total)
Reply To: org.hibernate.LazyInitializationException

You must be logged in to post in the forum log in