2016-04-21 80 views
0

Spring Transaction Hibernate @Transaction注释与@Autowired正确无效。如果我创建了Dao`` element by XML(UserDao2 userDao2)`,Spring Transaction Hibernate @Transaction注释不能与@Autowired配合使用

在服务类中的事务anotation工作,如果它通过在DAO类@Repository注释当试图getCurrentSesion,说:

org.hibernate.HibernateException: No Session found for current thread 
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) ~[spring-orm-3.2.8.RELEASE.jar:3.2.8.RELEASE] 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:993) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final] 

它看来,不与会话工厂缝好了@Transactional注释

库版本:

<jdk.version>1.6</jdk.version> 
<spring.version>3.2.8.RELEASE</spring.version> 
<spring.security.version>3.2.3.RELEASE</spring.security.version> 
<hibernate.version>4.2.11.Final</hibernate.version> 

弹簧database.xml

<context:annotation-config /> 

<jee:jndi-lookup id="datasourcenn" jndi-name="java:/comp/env/nn_datasource" /> 

    <bean id="sesionHibernate" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

     <property name="dataSource" ref="datasourcenn"/> 

     <property name="packagesToScan" value="com.web.entity"/> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>    
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.show_sql">true</prop> 

         <!-- nuevas properties de configuración --> 
       <prop key="hibernate.c3p0.min_size">5</prop> 
       <prop key="hibernate.c3p0.max_size">50</prop> 
       <prop key="hibernate.c3p0.timeout">300</prop> 
       <prop key="hibernate.c3p0.max_statements">50</prop> 

       <prop key="hibernate.bytecode.provider">cglib</prop>          
      </props> 
     </property> 
    </bean>  

    <bean id="us" class="com.web.dao.UserDaoImpl"> 
     <property name="sesionHibernate" ref="sesionHibernate" /> 
    </bean> 
    <!-- --> 
    <bean id="userDao2" class="com.web.dao.UserDao2Impl"> 
     <property name="sesionHibernate" ref="sesionHibernate" /> 
    </bean> 



    <bean id="myUserDetailsService" class="com.web.service.UsuarioServiceImpl"> 
     <property name="userDao" ref="us" /> 
    </bean> 
    <!-- 
    proxy-target-class="true" mode="aspectj" 
    --> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sesionHibernate"></property> 

    </bean> 

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

UserDao2(我删除@Repository如果我通过XML创建它)

@Repository 
public class UserDao2Impl implements UserDao2 { 
    private static Logger log = LoggerFactory.getLogger(UserDao2Impl.class); 

    @Autowired 
    @Qualifier("sesionHibernate") 
    private SessionFactory sesionHibernate; 

    @SuppressWarnings("unchecked") 
    public Usuario findByUserName(String username) { 
    try { 
     log.info("findByUserName" + sesionHibernate); 

     List<Usuario> users = new ArrayList<Usuario>(); 
     System.out.println(sesionHibernate+"\n----------"); 

     users = sesionHibernate.getCurrentSession().createQuery("from Usuario where nombre=?").setParameter(0, username).list(); 
     // users = sesionHibernate.getCurrentSession().createQuery("from Usuario 
     // where nombre=?").setParameter(0, username).list(); 

     if (users.size() > 0) { 
     return users.get(0); 
     } else { 
     return null; 
     } 

    } catch (Exception e) { 
     log.error("findByUserName ", e); 
     return null; 
    } 
    } 

Usuario2ServiceImpl如果有@Transacional

服务
@Service 
@Transactional 
public class Usuario2ServiceImpl implements Usuario2Service { 
    private static Logger log = LoggerFactory.getLogger(Usuario2ServiceImpl.class); 

    //Qualifier("userDaoImpl") 
    @Autowired 
    private UserDao2 userDao2;  

    @Override 
    public com.web.entity.Usuario getUsuariodetalles(final String nombreUsuario) throws UsernameNotFoundException { 
    log.info("getUsuariodetalles - 1"); 
    System.out.println("ssss"+userDao2); 
    com.web.entity.Usuario usuario = userDao2.findByUserName(nombreUsuario);  

    log.info("getUsuariodetalles - 2"); 
    return usuario; 
    } 
+0

,你既有'@ Service'和''。 – zeroflagL

+0

不,该bean用于另一个类UsuarioServiceImpl,@Service用于UsuarioService2Impl – Andreu

+0

让我猜测有一个'ContextLoaderLIstener'加载'spring-database.xml'并且您有一个DispatcherServlet',它加载了其他的东西包含一个''.... –

回答

0

解决方案,我添加到mvc-dispatcher-servlet.xml,mvc:拦截器配置以打开与org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor的会话。只需要在属性中定义sessionFactory。

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-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/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     "> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.web" /> 

    <mvc:interceptors> 
     <mvc:interceptor> 
      <mvc:mapping path="/**"/> 
      <bean name="OpenSessionInViewInterceptorCom" class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor"> 
       <property name="sessionFactory"> 
        <ref bean ="sesionHibernate" /> 
       </property> 
      </bean> 
     </mvc:interceptor> 
    </mvc:interceptors> 

    <!-- Configures the @Controller programming model --> 

    <mvc:annotation-driven /> 
    <mvc:resources mapping="/css/**" location="/css/" /> 
    <mvc:resources mapping="/img/**" location="/img/" /> 
    <mvc:resources mapping="/js/**" location="/js/" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/pages/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 



</beans> 
0

您是否尝试将@Transactional注释放在服务层方法上,您已将它放在类上。我认为它应该工作。

+0

我试图把@Transactional放在服务的接口上,在调用的函数上,在函数的接口上,到处都是,我仍然得到同样的错误。 – Andreu

0

我在Tomcat的解决了这个问题由spring-database.xml把下一行:

<context:load-time-weaver aspectj-weaving="autodetect"/> 

不过,这并不在JBoss中,这是我需要部署的最终版本。其他方案?

在Jboss中,我需要在xml(Service和Dao)中创建两个事物,保留事务注释。但是,如果使用@Repository或@Service创建它们(dao &服务),我无法工作。

相关问题