sessionFactory.getCurrentSession()返回不同的会话,但配置了OSIV

时间:2012-08-29 06:38:45

标签: spring hibernate open-session-in-view

我遇到了 Spring OpenSessionInViewFilter 的问题。我的 web.xml 配置如下所示:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/applicationContext.xml
    </param-value>
</context-param>


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

<filter>
    <filter-name>Hibernate-OSIV pattern</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>flushMode</param-name>
        <param-value>AUTO</param-value>
    </init-param>

</filter>
<filter-mapping>
    <filter-name>Hibernate-OSIV pattern</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我的通用 DAO 看起来像这样:

公共类BasicDaoImpl实现BasicDao {

private Class currentClass;

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Transactional(readOnly = true)
@Override
public T getById(long id) {
    Session session = getCurrentSession();
    return (T) session.get(getGenericClass(), id);
}

@Transactional(readOnly = true)
@Override
public List<T> getAll() {
    Session session = getCurrentSession();
    return session.createCriteria(getGenericClass()).list();
}

@Transactional(readOnly = false)
@Override
public void create(T entity) {
    Session session = getCurrentSession();
    session.save(entity);
}

@Transactional(readOnly = false)
@Override
public void update(T entity) {
    getCurrentSession().update(entity);
}

@Transactional(readOnly = false)
@Override
public void delete(T entity) {
    Session session = getCurrentSession();
    session.delete(entity);
}

@Override
@Transactional(readOnly = false)
public void update(Collection<T> entities) {
    Session session = getCurrentSession();
    for (T entity : entities) {
        session.update(entity);
    }
}

@Override
@Transactional(readOnly = false)
public void create(List<T> entities) {
    Session session = getCurrentSession();
    for (T entity : entities) {
        session.save(entity);
    }
}

private Class getGenericClass() {
    if (currentClass == null) {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        currentClass = (Class) type.getActualTypeArguments()[0];
    }
    return currentClass;
}

protected Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}
}

SessionFactory 被注入)

我知道它没有配置为单个会话(比如使用更新而不是合并),但首先我要摆脱 LazyInitializationException

我可以从调试中看到,每个请求上的 sessionFactory.getCurrentSession()都会返回具有不同 hashCode 的会话

现在我做错了什么?

1 个答案:

答案 0 :(得分:0)

从Hibernate 4.1.7开始,您可以将延迟关联上的事务作为配置属性关闭。 more details