---
title: "Hibernate Open-Session-In-View Pattern"
author: "Acea"
date: "2009-03-10T09:40:08Z"
tags: []
slug: "hibernate-open-session-in-view-pattern"
draft: false
lastmod: "2009-03-10T09:41:28Z"
id: 346319
url: "https://acea.iteye.com/blog/346319"
---

用hibernate，继续写代码来处理关联关系很繁，考虑用关联映射。

一用就是臭名昭著的 LazyInitializationException，于是参考hibernate的best practice使用Open-Session-In-View方式。

 

[http://www.jroller.com/cardsharp/entry/open\_session\_in\_view\_pattern](http://www.jroller.com/cardsharp/entry/open_session_in_view_pattern)

 

这篇文章说得实在：在表现层暴露back-end的处理机制总觉得不妥当，但是用起来这么透明、这么简便，那就用着吧 :-)

 

至于性能，网上讨论很多。从理论上分析，是有影响的。但是，影响到多大程度？在超过什么样的压力阈值之后会出现问题？

 

对于企业内部的应用，不超过10,000个用户，应该不至于成为瓶颈。

 

毕竟，现在的应用软件开发对开发速度、代码的可维护性等方面的要求更为挑战。

 

ps:

 

配置中：

org.springframework.orm.hibernate.support.OpenSessionInViewFilter

                                         ~~~~~~~

需要修改为

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

 

遇到几个配置的地方，都会因为少一个3导致 class not found.

 

 

#### Configuring Open-Session-In-View

Here's a excerpt from a simple web.xml that loads Spring configuration files from the classpath and activates the Open-Session-in-View for a Tapestry application:

```
<web-app>
  <!-- web-app settings go here -->

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:/com/yourcompany/daopackage/applicationContext-hibernate.xml,
      classpath:/com/yourcompany/servicepackage/applicationContext-service.xml
    </param-value>
  </context-param>

  <!-- Spring Open Session In View Pattern filter -->
  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
      org.springframework.orm.hibernate.support.OpenSessionInViewFilter
    </filter-class>
  </filter>

  <!-- Spring/Hibernate filter mappings -->
  <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
  </filter-mapping>

  <!-- Listeners -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <!-- Servlet mappings & other config -->
</web-app>
```