1

我正在尝试在 JSF2-Spring-Hibernate Web 应用程序中实现 Session-Per-Conversation 模式,因此我需要 AnnotationSessionFactoryBean 来构建具有自定义 CurrentSessionContext 类的 Hibernate SessionFactory。

我一直在收到此错误日志:

org.springframework.dao.DataAccessResourceFailureException: Could not obtain Hibernate-managed Session for Spring-managed transaction; nested exception is org.hibernate.HibernateException: No CurrentSessionContext configured!

这是我使用的整个数据上下文的 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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}" />
        <property name="url" value="${datasource.url}" />
        <property name="username" value="${datasource.username}" />
        <property name="password" value="${datasource.password}" />
        <property name="initialSize" value="${datasource.poolInitialSize}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${org.hibernate.dialect.dialectmysqlInno}</prop>
                <prop key="hibernate.hbm2ddl.auto">${org.hibernate.ddl.mode}</prop>
                <prop key="hibernate.search.default.directory_provider">${org.hibernate.search.directoryprovidr}</prop>
                <prop key="hibernate.current_session_context_class">
                    mx.gob.jgtjo.apps.schedule.web.conversation.ConversationalCurrentSessionContext
                </prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateManagedSession" value="true" />
    </bean>

    <tx:annotation-driven order="0" transaction-manager="transactionManager" />

    <context:component-scan base-package="mx.gob.jgtjo.apps.schedule.dao.hibernate" />

</beans>

另外,这是我的 hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="jgtjoSessionFactory">
        <!--Entity -->
        <mapping class="mx.gob.jgtjo.apps.schedule.model.AudienciaOral" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.CausaPenal" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.DefensorPenal" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.Delito" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.EventoAudiencia" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.Juez" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.MinisterioPublico" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.ParteMaterial" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.Sala" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.TipoAudiencia" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.User" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.Rol" />
        <mapping class="mx.gob.jgtjo.apps.schedule.model.DelitoConfigurado" />
    </session-factory>
</hibernate-configuration>

如您所见,hibernate xml 没有什么棘手的。

为什么我不断收到此异常?

java.lang.NoSuchMethodException: mx.gob.jgtjo.apps.schedule.web.conversation.ConversationalCurrentSessionContext.<init>(org.hibernate.engine.SessionFactoryImplementor)

似乎休眠在我的类中寻找一个以 SessionFactory 作为参数的构造函数。

4

2 回答 2

2

这是来自 Hibernate 的代码,它尝试使用您使用该hibernate.current_session_context_class属性传入的任何值来构建当前会话上下文:

private CurrentSessionContext buildCurrentSessionContext() {
        String impl = properties.getProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS );
        // for backward-compatability
        if ( impl == null && transactionManager != null ) {
            impl = "jta";
        }

        if ( impl == null ) {
            return null;
        }
        else if ( "jta".equals( impl ) ) {
            if ( settings.getTransactionFactory().areCallbacksLocalToHibernateTransactions() ) {
                log.warn( "JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()" );
            }
            return new JTASessionContext( this );
        }
        else if ( "thread".equals( impl ) ) {
            return new ThreadLocalSessionContext( this );
        }
        else if ( "managed".equals( impl ) ) {
            return new ManagedSessionContext( this );
        }
        else {
            try {
                Class implClass = ReflectHelper.classForName( impl );
                return ( CurrentSessionContext ) implClass
                        .getConstructor( new Class[] { SessionFactoryImplementor.class } )
                        .newInstance( new Object[] { this } );
            }
            catch( Throwable t ) {
                log.error( "Unable to construct current session context [" + impl + "]", t );
                return null;
            }
        }
    }

如您所见,可接受的值为jta, thread, managed

由于您使用的是 Spring 的事务管理功能,因此根本不应该设置此属性。Spring 会为你解决这个问题。

您只需要注释您的事务方法,@Transactional会话就会为您打开并绑定到当前线程。

于 2012-06-26T18:05:45.723 回答
1

好的,因为上面的答案向我展示了我在问题编辑中显示的代码和日志记录,CurrentSessionContext 接口的实现必须有一个以 sessionFactory 作为参数的公共构造函数。

Hibernate 文档从不这样说。

这是我的课:

    package mx.gob.jgtjo.apps.schedule.web.conversation;

import javax.servlet.http.HttpServletRequest;

import mx.gob.jgtjo.apps.schedule.web.utils.JsfUtils;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.context.CurrentSessionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConversationalCurrentSessionContext implements CurrentSessionContext {

    private static final long serialVersionUID = 803157986557235023L;

    protected static final Logger log = LoggerFactory
            .getLogger(ConversationalCurrentSessionContext.class);

    public ConversationalCurrentSessionContext() {

    }

    @Override
    public Session currentSession() throws HibernateException {

        HttpServletRequest request = null;

        try {
            request = JsfUtils.getCurrentHttpRequest();
        } catch (Exception e) {
            log.debug("No current http request in faces context, returning default conversation.");
        }

        if (request == null) {
            return (Session) ConversationManager.getDefaultConversationSession();
        } else {
            return (Session) ConversationManager.getSessionForRequest(request);
        }
    }
}

如您所见,我缺少该构造函数。

于 2012-06-27T01:52:16.037 回答