2

我对SpringOpenSessionInViewFilter有疑问。我的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的会话

现在我做错了什么?

4

1 回答 1

0

从 Hibernate 4.1.7 开始,您可以将惰性关联上的事务作为配置属性关闭。更多细节

于 2013-01-17T07:59:08.460 回答