2016-12-15 4 views
3

Я работаю над простой демонстрацией Hibernate на сервере FUSE, и я нашел этот пример кода примера в этом github location. Я пытаюсь запустить этот код в FUSE 6.3 с интеграцией Hibernate 4. Я смог установить все необходимые зависимости, но когда я пытаюсь выполнить операцию базы данных, я получаю следующую ошибку.FUSE hibernate application throws Нет сеанса для текущего исключения потока

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:997) 
at com.vcc.poc.dao.impl.StudentDaoImpl.getStudent(StudentDaoImpl.java:67) 
at sun.reflect.GeneratedMethodAccessor242.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111] 
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111] 
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:62)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:56)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:39)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:59)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at com.sun.proxy.$Proxy93.getStudent(Unknown Source) 
at com.vcc.poc.service.impl.StudentServiceImpl.getStudent(StudentServiceImpl.java:15) 
at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111] 
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111] 
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at com.sun.proxy.$Proxy94.getStudent(Unknown Source) 
at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111] 
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111] 
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:62)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:56)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:39)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.osgi.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:59)[org.springframework.osgi:spring-osgi-core:1.2.1] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1] 

Это мое содержание в файле StudentImpl.java.

package com.vcc.poc.dao.impl; 

    import java.util.List; 

    import org.apache.commons.logging.Log; 
    import org.apache.commons.logging.LogFactory; 
    import org.hibernate.HibernateException; 
    import org.hibernate.Query; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.internal.SessionImpl; 

    import com.vcc.poc.dao.StudentDao; 
    import com.vcc.poc.model.Student; 

    public class StudentDaoImpl implements StudentDao{ 

    private static final transient Log LOG = LogFactory.getLog(StudentDaoImpl.class); 

     /** The session factory. */ 
     private SessionFactory sessionFactory; 

     /** The q. */ 
     private Query q = null; 

     /** The Constant findStudentByReference. */ 
     private final static String findStudentByReference = 
      "select i from Student as i where i.studentRef = :ref"; 

     /** The Constant findStudent. */ 
     private final static String findStudent = 
      "select i from Student as i"; 

     /** 
     * Sets the session factory. 
     * 
     * @param sessionFactory the new session factory 
     */ 
     public void setSessionFactory(SessionFactory sessionFactory) 
     { 
      this.sessionFactory = sessionFactory; 
     } 

     /* 
     * (non-Javadoc) 
     * @see org.apache.camel.example.reportincident.dao.IncidentDAO#findIncident() 
     */ 
     public List<Student> findStudent() 
      throws HibernateException 
     { 

      // Prepare query 
      q = this.sessionFactory.getCurrentSession().createQuery(findStudent); 

      // Retrieve the Incidents from database 
      List<Student> list = q.list(); 

      return list; 

     } 

     /* 
     * (non-Javadoc) 
     * @see org.apache.camel.example.reportincident.dao.IncidentDAO#getIncident(long) 
     */ 
     public Student getStudent(int id) 
     { 
      return (Student) this.sessionFactory.getCurrentSession().get(Student.class, id); 
     } 


     /* 
     * (non-Javadoc) 
     * @see org.apache.camel.example.reportincident.dao.IncidentDAO#saveIncident(org.apache.camel.example.reportincident.model.Incident) 
     */ 
     public void saveStudent(Student Student) 
     { 
      SessionImpl session = (SessionImpl) this.sessionFactory.getCurrentSession(); 
      this.sessionFactory.getCurrentSession().saveOrUpdate(Student); 
     } 


    } 

Согласно исключением следующего является линия его бросить исключение

return (Student) this.sessionFactory.getCurrentSession().get(Student.class, id); 

Это мои данные источника-beans.xml в META-INF папку/пружинного

<?xml version="1.0" encoding="UTF-8"?> 
    <!-- 
     Licensed to the Apache Software Foundation (ASF) under one or more 
     contributor license agreements. See the NOTICE file distributed with 
     this work for additional information regarding copyright ownership. 
     The ASF licenses this file to You under the Apache License, Version 2.0 
     (the "License"); you may not use this file except in compliance with 
     the License. You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

     Unless required by applicable law or agreed to in writing, software 
     distributed under the License is distributed on an "AS IS" BASIS, 
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     See the License for the specific language governing permissions and 
     limitations under the License. 
    --> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:osgi="http://www.springframework.org/schema/osgi" 
     xmlns:osgix="http://www.springframework.org/schema/osgi-compendium" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/osgi 
      http://www.springframework.org/schema/osgi/spring-osgi.xsd 
      http://www.springframework.org/schema/osgi-compendium 
      http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd"> 

     <context:annotation-config /> 
     <context:property-placeholder properties-ref="preProps" /> 

     <!-- DB connection and persistence layer --> 
     <!-- DataSource Definition --> 
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
      <property name="driverClassName" value="${driverClassName}" /> 
      <property name="url" value="${url}" /> 
      <property name="username" value="${username}" /> 
      <property name="password" value="${password}" /> 
     </bean> 


     <!-- Spring Data Access Exception Translator Definition --> 
     <bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"> 
      <property name="dataSource"> 
       <ref bean="dataSource" /> 
      </property> 
     </bean> 

     <!-- Hibernate SessionFactory Definition --> 
     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

      <property name="mappingLocations"> 
       <list> 
        <value>classpath*:META-INF/com/vcc/poc/model/*.hbm.xml</value> 
       </list> 
      </property> 

      <property name="hibernateProperties"> 
       <props> 
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
        <prop key="hibernate.show_sql">false</prop> 
        <prop key="hibernate.format_sql">true</prop> 
        <prop key="hibernate.cglib.use_reflection_optimizer">true</prop> 
        <prop key="hibernate.jdbc.batch_size">10</prop> 
        <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop> 
        <!-- 
        <prop key="hibernate.transaction.manager_lookup_class">org.springframework.orm.hibernate3.LocalTransactionManagerLookup</prop> 
        <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> 
        --> 
       </props> 
      </property> 
      <property name="dataSource"> 
       <ref bean="dataSource" /> 
      </property> 
      <!-- 
      <property name="jtaTransactionManager"> 
       <osgi:reference> 
       <osgi:interfaces> 
        <value>org.springframework.transaction.PlatformTransactionManager</value> 
        <value>javax.transaction.TransactionManager</value> 
       </osgi:interfaces> 
       </osgi:reference> 
      </property> 
      --> 

     </bean> 

     <!-- Hibernate Transaction Manager Definition --> 
     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <property name="sessionFactory"> 
       <ref local="sessionFactory" /> 
      </property> 
     </bean> 


     <!-- 
      Hibernate Transaction Manager exported as OSGI service because used 
      org.apache.camel.example.reportincident.service 

     <osgi:service id="transactionManagerOsgi" ref="transactionManager" 
      interface="org.springframework.transaction.PlatformTransactionManager" /> 
      --> 

     <osgix:cm-properties id="preProps" persistent-id="com.vcc.poc.dao.datasource"> 
      <prop key="driverClassName">com.mysql.jdbc.Driver</prop> 
      <prop key="url">jdbc:mysql:///demo</prop> 
      <prop key="username">root</prop> 
      <prop key="password">password</prop>   
     </osgix:cm-properties> 



    </beans> 

Это структура проекта, которую у меня есть.

enter image description here

ли кто-нибудь испытывал такого рода вопросов на FUSE зимуют интеграции. Я смог установить проект без каких-либо проблем. Но когда я пытаюсь выполнить операцию базы данных, я получаю эту ошибку.

Возможно, какой-то эксперт поможет мне решить эту проблему.

ответ

1

Вы можете решить это, изменив следующие действия.

Добавить следующую схему в Избранное в тегах beans в соответствии со следующим сообщением.

http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 

В туда вы должны добавить следующие строки. На основе following post.

<tx:annotation-driven/> 
<context:annotation-config /> 
<context:component-scan base-package="com.vcc.poc"></context:component-scan> 

Затем добавить аннотацию @Transactional для методов, например

@Transactional 
    public Student getStudent(int id) 

Изменено фасоль аннотаций должны быть аналогичны ниже.

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:osgi="http://www.springframework.org/schema/osgi" 
    xmlns:osgix="http://www.springframework.org/schema/osgi-compendium" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/osgi 
     http://www.springframework.org/schema/osgi/spring-osgi.xsd 
     http://www.springframework.org/schema/osgi-compendium 
     http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd 

     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
     "> 

    <tx:annotation-driven/> 
    <context:annotation-config /> 
    <context:property-placeholder properties-ref="preProps" /> 
    <context:component-scan base-package="com.vcc.poc"></context:component-scan> 
Смежные вопросы