Home >Database >Mysql Tutorial >Hibernate+Spring数据延迟加载问题解决方案

Hibernate+Spring数据延迟加载问题解决方案

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:10:411127browse

第一种:更改lazy属性为:false 第二种:加 lt;filtergt; lt;filter-namegt;OpenSessionInViewFilterlt;/filter-namegt;

第一种:更改lazy属性为:false

第二种:加
   OpenSessionInViewFilter
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  
    flushMode
    AUTO
   

  

  
   OpenSessionInViewFilter
   /*
  

以上两种方法都有点问题。

第三种是自己写代码,手动加载

代码如下:

public class LazyDaoImpl extends HibernateDaoSupport implements LazyDao {

 /* (non-Javadoc)
  * @see com.chinarck.DAO.LazyDao#loadContractService(java.io.Serializable)
  */
 @Override
 public Object loadContractService(Serializable id) {
  final int idd=(Integer) id;
//  Entercontract e= getHibernateTemplate().get(Entercontract.class, id);
//  getHibernateTemplate().initialize(e.getService());
  Entercontract e=getHibernateTemplate().execute(new HibernateCallback(){
   public Object doInHibernate(Session session)throws HibernateException,SQLException{
    //System.out.println(idd);
    Entercontract ee=(Entercontract) session.get(Entercontract.class,idd);
    //System.out.println(ee.getConId());
    Hibernate.initialize(ee);
    ee.getService();
    //System.out.print(ee.getService().size());
    return ee;
   
   }
  });
  return e;
 }


 }

注意:一定要用get方法。load也不行。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn