web.xml中openEntityManagerInViewFilter的作用

來源:互聯網
上載者:User
<!-- Open Entity Manager in View filter--><filter><filter-name>openEntityManagerInViewFilter</filter-name><filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class></filter><filter-mapping><filter-name>openEntityManagerInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>


在Java Web項目中使用Hibernate經常會遇到LazyInitializationException。這是因為controller和model層(java代碼)將通過JPA的一些啟用了消極式載入功能的領域(如用getRefrence()方法或者在關聯關係中採用fetch=FetchType.LAZY)返回給view層(jsp代碼)的時候,由於載入領域對象的JPA
Session已經關閉,導致這些消極式載入的資料訪問異常。

這時就可以使用OpenEntityManagerInViewFilter來將一個JPAsession與一次完整的請求過程對應的線程相綁定。請看一段虛擬碼:

    Service{              entitymanager=context.getEntityManager();              if(entitymanager==null) context.put(factory,createEntityManager());              entitymangager=context.getEntityManager();              entitymanager.begin();              public void find(Integer productid){//除了這個方法外,其他都是通過AOP織入的                  em.getReference(Product.class,productid));              }              entitymanager=context.getEntityManager();              entitymanager.commit();              entitymanager.close();      }  

上面的虛擬碼示範了如果我們想調用我們自己定義的一個find()方法,spring會在該方法的前後織入一些代碼來開始事物和關閉session。當view層調要用這個find()方法擷取的對象(由於採用了消極式載入模式,只有到要使用到該對象的時候才會讓session去資料庫取)的時候,實際上session已經關閉了,不能再讓session擷取對象。

OpenEntityManagerInViewFilter會讓session一直到view層調用結束後才關閉,請看下面的虛擬碼:

    Filter{              doFilter(chain){                  context.getEntityManager().open();                  chain.doFilter(req,res){                      xxxAction{                          execute(){                              Product product=service.find(productid);//此時該對象為游離狀態,實際上並沒有在資料庫獲得值。                              req.setAttibute("Product",produxt);//這時候才到資料庫裡面去取值                              return mapping.findForward("product");                          }                      }                  }                  context.getEntityManager().close()...              }          }  

如果沒使用OpenEntityManagerInViewFilter,session會在service.find()方法後就被關閉,用了以後session在整個view層結束後才關閉。

摘自:http://whoosh.iteye.com/blog/1300721



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.