1

JSF2-Spring-Hibernate Web App で Session-Per-Conversation パターンを実装しようとしているので、カスタム CurrentSessionContext クラスで Hibernate SessionFactory を構築するには AnnotationSessionFactoryBean が必要です。

次のエラー ログを受け取りました。

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.current_session_context_classこれは、プロパティを使用して渡した値を使用して現在のセッション コンテキストを構築しようとする Hibernate のコードです。

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;
            }
        }
    }

ご覧のとおり、許容値はjtathreadmanagedです。

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 に答える