Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate-getCurrentSession-get在没有活动事务的情况下无效_Java_Hibernate - Fatal编程技术网

Java hibernate-getCurrentSession-get在没有活动事务的情况下无效

Java hibernate-getCurrentSession-get在没有活动事务的情况下无效,java,hibernate,Java,Hibernate,我正在使用java命令行应用程序测试hibernate 4.1.9。我已将当前会话上下文配置为线程: <property name="hibernate.current_session_context_class">thread</property> 我可以使用openSession,这并不重要(毕竟这是一个测试)。我只是好奇为什么我不能让方法getCurrentSession像广告那样工作 谢谢。对sessionFactory.getCurrentSession()的

我正在使用java命令行应用程序测试hibernate 4.1.9。我已将当前会话上下文配置为线程:

<property name="hibernate.current_session_context_class">thread</property>
我可以使用
openSession
,这并不重要(毕竟这是一个测试)。我只是好奇为什么我不能让方法
getCurrentSession
像广告那样工作


谢谢。

对sessionFactory.getCurrentSession()的第一次调用将返回一个新会话。问题在于我的配置

我是这样的:

<property name="hibernate.current_session_context_class">thread</property>
线程
将其更改为此后,它工作正常:

<property name="current_session_context_class">thread</property>
线程

这是一个有点老的问题,但我想正确回答

通过将属性名称
hibernate.current\u session\u context\u class
更改为
current\u session\u context\u class
,强制默认
jtasesessioncontext

下面的代码片段来自hibernate
SessionFactoryImpl
。顺便说一句,
Environment.CURRENT\u SESSION\u CONTEXT\u CLASS
“hibernate.CURRENT\u SESSION\u CONTEXT\u CLASS”
ThreadLocalSessionContext
导致此问题

private CurrentSessionContext buildCurrentSessionContext() {
        String impl = (String) properties.get( Environment.CURRENT_SESSION_CONTEXT_CLASS );
        // for backward-compatibility
        if ( impl == null ) {
            if ( canAccessTransactionManager() ) {
                impl = "jta";
            }
            else {
                return null;
            }
        }

        if ( "jta".equals( impl ) ) {
//          if ( ! transactionFactory().compatibleWithJtaSynchronization() ) {
//              LOG.autoFlushWillNotWork();
//          }
            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 = serviceRegistry.getService( ClassLoaderService.class ).classForName( impl );
                return (CurrentSessionContext)
                        implClass.getConstructor( new Class[] { SessionFactoryImplementor.class } )
                        .newInstance( this );
            }
            catch( Throwable t ) {
                LOG.unableToConstructCurrentSessionContext( impl, t );
                return null;
            }
        }
    }

请检查
ThreadLocalSessionContext.TransactionProtectionWrapper.invoke

是否确实解决了此问题?我认为这不是此问题的解决方案。我在Hibernate 4.2.8上遇到了相同的问题,这解决了我的问题@hba我已经厌倦了您的ans并解决了问题,但我想知道这是怎么可能的?。如果你能解释的话,请告诉我……很抱歉反应迟迟。我很久以前就处理过这个问题,现在开始处理了…我记不清了,无法再回答这个问题了…对不起
private CurrentSessionContext buildCurrentSessionContext() {
        String impl = (String) properties.get( Environment.CURRENT_SESSION_CONTEXT_CLASS );
        // for backward-compatibility
        if ( impl == null ) {
            if ( canAccessTransactionManager() ) {
                impl = "jta";
            }
            else {
                return null;
            }
        }

        if ( "jta".equals( impl ) ) {
//          if ( ! transactionFactory().compatibleWithJtaSynchronization() ) {
//              LOG.autoFlushWillNotWork();
//          }
            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 = serviceRegistry.getService( ClassLoaderService.class ).classForName( impl );
                return (CurrentSessionContext)
                        implClass.getConstructor( new Class[] { SessionFactoryImplementor.class } )
                        .newInstance( this );
            }
            catch( Throwable t ) {
                LOG.unableToConstructCurrentSessionContext( impl, t );
                return null;
            }
        }
    }