Spring + Hibernate:在@Transactional方法中找不到会话

时间:2013-08-21 14:25:40

标签: spring hibernate

我已经阅读了数十个主题而没有结果。

问题:

我无法在@Transactional方法中获得当前的hibernate会话。

例外:

Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
    at name.krestjaninoff.activiti.hello.core.Engine.getHibernateSession(Engine.java:27)
    at name.krestjaninoff.activiti.hello.process.CreateClientService.execute(CreateClientService.java:42)
    at org.activiti.engine.impl.bpmn.JavaDelegateDelegate.execute(JavaDelegateDelegate.java:48)
    at org.activiti.engine.impl.bpmn.JavaDelegateDelegate.execute(JavaDelegateDelegate.java:39)
    at org.activiti.engine.impl.bpmn.ClassDelegate.execute(ClassDelegate.java:96)
    at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:40)
....

我的代码:

@Service
@EnableTransactionManagement(proxyTargetClass=true, mode= AdviceMode.PROXY)
public class CreateClientService {

  @Transactional(value="transactionManager", propagation = Propagation.REQUIRED)
  public void execute() {
    ((SessionFactory) applicationContext.getBean("sessionFactory")).getCurrentSession(); //EXCEPTOIN!!!
  }
}

我的配置:

<tx:annotation-driven/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>name.krestjaninoff.activiti.hello.db</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop>
                <!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" scope="singleton">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>

ADDTION:NativeConstructorAccessorImpl类创建CreateClientService实例:

@CallerSensitive
public T newInstance(Object ... initargs)
    throws InstantiationException, IllegalAccessException,
           IllegalArgumentException, InvocationTargetException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, null, modifiers);
        }
    }
    if ((clazz.getModifiers() & Modifier.ENUM) != 0)
        throw new IllegalArgumentException("Cannot reflectively create enum objects");
    ConstructorAccessor ca = constructorAccessor;   // read volatile
    if (ca == null) {
        ca = acquireConstructorAccessor();
    }
    return (T) ca.newInstance(initargs);
}ected JavaDelegate javaDelegate;

  protected JavaDelegateDelegate() {
  }

  public JavaDelegateDelegate(JavaDelegate javaDelegate) {
    this.javaDelegate = javaDelegate;
  }

  public void execute(ActivityExecution execution) throws Exception {
    execute((DelegateExecution) execution);
    performDefaultOutgoingBehavior(execution);
  }

  public void notify(ExecutionListenerExecution execution) throws Exception {
    execute((DelegateExecution) execution);
  }

  public void execute(DelegateExecution execution) throws Exception {
    javaDelegate.execute(execution);
  }
}

0 个答案:

没有答案