当前位置: 代码迷 >> J2EE >> org.hibernate.HibernateException: No CurrentSessionContext configured!解决方案
  详细解决方案

org.hibernate.HibernateException: No CurrentSessionContext configured!解决方案

热度:261   发布时间:2016-04-19 22:44:25.0
org.hibernate.HibernateException: No CurrentSessionContext configured!
Beans.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  <context:annotation-config />
  <context:component-scan base-package="com.Alvin_yau"/>
  
  <bean id="addtionInterceptor" class="com.Alvin_yau.Spring.AOP.AddtionInterceptor"/>
  
  <aop:config>
        <aop:aspect id="addtionAspect" ref="addtionInterceptor">
             <aop:before method="Before" pointcut="execution(public * com.Alvin_yau.Spring.Service..*.save(..))"/>
        </aop:aspect>
  </aop:config>
  
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
  
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
   <property name="packagesToScan">
<list>
<value>com.Alvin_yau.Spring.Model</value>
</list>
</property>
  <property name="hibernateProperties">
    <props>
<prop key="hibernate.dialect">
 org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">
     org.springframework.orm.hibernate4.SpringSessionContext
</prop>
              
</props>

  </property>
</bean>

<bean name="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      <property name="dataSource" ref="dataSource" />
      <property name="sessionFactory" ref="sessionFactory"></property>  
</bean>  
 <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven> 

</beans>


————————————————————————————————————————————————
————————————————————————————————————————————————

@Component(value="userDao")
public class UserDaoImpl implements UserDao {
     
private SessionFactory sessionFactory=null;

public SessionFactory getSessionFactory() {
return sessionFactory;
}
    @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public void add(Student student) {
// TODO Auto-generated method stub
   Session session=sessionFactory.getCurrentSession();
   session.save(student);
}
}


@Component(value="logDao")
public class LogDaoImpl implements LogDao {
    private SessionFactory sessionFactory;
   
public SessionFactory getSessionFactory() {
return sessionFactory;
  相关解决方案