Lazy在Spring MVC中初始化

时间:2011-08-15 12:26:58

标签: hibernate spring spring-mvc

尽管使用下面的配置,我得到了延迟初始化错误:

  

org.hibernate.LazyInitializationException:懒得初始化角色集合:beans.Restaurant.tags,没有关闭会话或会话

在这里

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>RestFinderWebApp</display-name>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <!-- <welcome-file>index.html</welcome-file> -->
</welcome-file-list>
<!-- Spring security: -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-security.xml,
        /WEB-INF/spring-servlet.xml
    </param-value>
</context-param>

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

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

弹簧servlet.xml中:

    <?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.jmx.export.MBeanExporter">
        <property name="autodetect" value="false" />
        <property name="assembler">
            <bean id="jmxAssembler"
                class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
                <property name="attributeSource">
                    <bean
                        class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
                </property>
            </bean>
        </property>

    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="messages" />

    <bean id="openSessionInViewInterceptor"
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="myPersistence" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="generateDdl" value="true" /> 
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            </bean>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        (...)
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                (...)
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <tx:annotation-driven />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="myUserDAO" class="dao.UserDAO">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="myRestaurantDAO" class="dao.RestaurantDAO">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean name="/mojekonto.htm" class="web.HomeUserController">
        <property name="userDAO" ref="myUserDAO" />
        <property name="restaurantDAO" ref="myRestaurantDAO" />
        <property name="loggedUser" ref="LoggedUser" />
    </bean>

</beans>

我的控制员:

public class HomeUserController extends MultiActionController {
private UserDAO userDAO;
private TagDAO tagDAO;

public void setTagDAO(TagDAO tagDAO) {
    this.tagDAO = tagDAO;
}

public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
}

private LoggedUser loggedUser;

public void setLoggedUser(LoggedUser loggedUser) {
    this.loggedUser = loggedUser;
}

private RestaurantDAO restaurantDAO;


public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
    this.restaurantDAO = restaurantDAO;
}

private FollowDAO followDAO;

public void setFollowDAO(FollowDAO followDAO) {
    this.followDAO = followDAO;
}
private CommentDAO commentDAO;


public void setCommentDAO(CommentDAO commentDAO) {
    this.commentDAO = commentDAO;
}
@InitBinder
protected void initBinder(HttpServletRequest request,
        ServletRequestDataBinder binder) throws Exception {

    super.initBinder(request, binder);

    binder.registerCustomEditor(List.class, "tags",new CustomCollectionEditor(List.class){

        @Override
        protected Object convertElement(Object element) {
            Tag tag = new Tag();

            if (element != null) {
                int id = Integer.valueOf(element.toString());
                tag.setId(id);
            }
            return tag;
        }
    });

}
public ModelAndView mojekonto(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    User user = loggedUser.getLoggedUser();
    ModelMap modelMap = new ModelMap();
    Restaurant restaurant = new Restaurant();
    Restaurant userRestaurant = new Restaurant();
    userRestaurant = restaurantDAO.findRestaurantByUser(user.getUsername());

    modelMap.addAttribute("user", user);
    modelMap.addAttribute("restaurant", restaurant);
    modelMap.addAttribute("userRestaurant", userRestaurant);

    return new ModelAndView("mojekonto", modelMap);
}

我的班级 - 餐厅:

    @Entity
@Table(name="restaurants")
public class Restaurant {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String street;
    private String city;
    private String country;
    private String postal_code;
    private String telephone;
    private String logo;
    private String image;
    private String description;
    private float longitude;
    private float latitude;
    private Date last_update;
    @ManyToMany //it works fine...
    @JoinTable(name="user_restaurant_owner",
            joinColumns={@JoinColumn(name="restaurant_id")},
            inverseJoinColumns={@JoinColumn(name="username")})
    private List<User> owner;
    @ManyToMany //but that doesn't...
    @JoinTable(name="restaurant_tag",
            joinColumns={@JoinColumn(name="restaurant_id")},
            inverseJoinColumns={@JoinColumn(name="tag_id")})
    private List<Tag> tags;
        //getters and setters:

我的观点jsp:

 <c:forEach items="${userRestaurant.tags}" var="current">
do something
</c:forEach>

我在“{userRestaurant.tags}”

中遇到错误

3 个答案:

答案 0 :(得分:3)

使用SpringOpenEntityManagerInViewFilter,您的配置看起来是正确的,因此您不必诉诸于急切的提取或必须预先遍历控制器中的集合。我在我正在开发的应用程序中有一个非常相似的配置和模式,它的工作没有问题。您可以尝试的一件事是向DispatcherServlet配置添加空白上下文位置。没有检查过,但我认为根据全局配置加载上下文有一些默认行为。这将导致调度程序servlet实际上加载一个单独的上下文而不是仅创建一个空白上下文,继承自ContextLoaderListener创建的上下文(这是你想要它做的)。

因此,对于DispatcherServlet的<servlet>声明,请添加:

<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
</init-param>

更新:使用hibernate事务管理器看起来有点奇怪(考虑到你使用的是JPA实体管理器)。尝试更改它以改为使用Jpa事务管理器。

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

更新2:再次查看您的配置,看起来您正在使用JPA和Hibernate的奇怪组合。例如,您定义了两个事务管理器。为什么?此外,您正在使用org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter(JPA / EntityManager),同时使用org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor(即Hibernate / Session)。你是如何在DAO中查找实体的?使用hibernate会话还是JPA EntityManager?坚持使用JPA或Hibernate以最小化冲突可能会更好。

我也很难让Open*InViewInteceptor工作。过滤器看起来更稳定(例如,如果你决定使用hibernate pure,你有org.springframework.orm.hibernate3.support.OpenSessionInViewFilter。)

答案 1 :(得分:2)

您只能访问DAO层中的Hibernate会话。如果您的关联没有配置FetchType,或者他们有FetchType.LAZY,那么您在DAO中有两个选择:

  1. 使用Hibernate.initialize
  2. 获取强制Hibernate初始化的关联的某些方面
  3. 示例1:Hibernate.initialize(myEntity.getMyAssociation());

    示例2:

    if(myEntity.getMyAssociation() != null) {
       myEntity.getMyAssociation().size(); // forces the association to be loaded
    }
    

    我通常使用示例2,因为那时我也可以利用BatchSize来使用Hibernate来优化延迟提取。

    -----------编辑1 -----------

    使用HibernateTemplate和回调的这种情况的示例:

       public Restaurant load(final Long id) {
        // need to drop down to Hibernate because of lazy loading, and make sure all properties are loaded
        HibernateCallback<Restaurant> callBack = new HibernateCallback<Restaurant>() {
            public User doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery("from Restaurant r where r.id=:id").setInt("id", id);
                Restaurant restaurant = (Restaurant)query.uniqueResult();
                Hibernate.initialize(restaurant.getOwner());
                Hibernate.initialize(restaurant.getTags());             
                return restaurant;
            }
        };
        return getHibernateTemplate().execute(callBack);
    

    }

    -----------编辑2 ----------- 以下是如何在Hibernate中直接进行的操作:

    @Repository
    public class RestaurantDAOImpl implements RestaurantDAO {
        private SessionFactory factory;
    
        @Autowired
        public RestaurantDAOImpl(SessionFactory factory) {
            this.factory = factory;
        }   
    
        public Restaurant load(Long id) {
           Query query = session.createQuery("from Restaurant r where r.id=:id").setInt("id", id);
           Restaurant restaurant = (Restaurant)query.uniqueResult();
           Hibernate.initialize(restaurant.getOwner());
           Hibernate.initialize(restaurant.getTags());
           /* --OR --
           if(restaurant.getTags() != null) {
              restaurant.getTags().size();
           }
           */           
           return restaurant;
        }
    }
    

答案 2 :(得分:0)

在引用它们之前尝试FetchType.EAGER或手动获取标记。

@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name="restaurant_tag",
joinColumns={@JoinColumn(name="restaurant_id")},
inverseJoinColumns={@JoinColumn(name="tag_id")})
private List<Tag> tags;