自己做ssh框架,报了下面的错,在网上找了很多资料,但还是没有解决,各位大哥,帮忙看下
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here下面是我的配置文件,第一个applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
 <!-- 格式化输出sql语句 -->
     <prop key="hibernate.format_sql">true</prop>
     <prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/ourcompany/dao/pojo</value>
</list>
</property>
</bean> <!-- 配置transactionFactory -->
<bean id="transactionFactory" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 配置事务代理 -->
<bean id="transactionproxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionFactory" />
<property name="transactionAttributes">
<props>
<prop key="set*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="get*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="save*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="validateUser*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-DataAccessException</prop>
</props>
</property>
</bean>

</beans>
-----------------------------------------------------------------------------------------------
第二个applicationContext-action.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userAction" name="/userAction" class="com.ourcompany.action.UserAction">
<property name="userBLL">
<ref bean="userBLL"/>
</property>
</bean>
</beans>
---------------------------------------------------------------------------------------
第三个applicationContext-common.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- 配置baseDAO -->
<bean id="baseDAO" class="com.ourcompany.common.BaseDAO">
<property name="sf">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
----------------------------------------------------------------------------------
第四个applicationContext-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userDAO" class="com.ourcompany.dao.UserDAOImpl">
<property name="sf">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
----------------------------------------------------------------------------------
第五个applicationContext-services.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userBLL" class="com.ourcompany.service.UserBLL">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>

<bean id="userBLLProxy" parent="transactionproxy">
<property name="target">
<ref local="userBLL"/>
</property>
</bean>
</beans>
-----------------------------------------------------
java代码 action类
public class UserAction extends DispatchAction{ private UserBLL userBLL; /**
 * @description    登录
 * @createTime     Sep 24, 2012
 * @lastUpdateTime Sep 24, 2012
 * @see
 */
public ActionForward loginMethod(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if(userBLL.validateUser((UserForm)form)){
return mapping.findForward("success");
}else{
return mapping.findForward("loginfail");
}
}

public void setUserBLL(UserBLL userBLL) {
this.userBLL = userBLL;
}java代码 业务层
public class UserBLL {

private UserDAOImpl userDAO; public boolean validateUser(UserForm userForm) {

try {
return userDAO.validateUser(userForm);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

public void setUserDAO(UserDAOImpl userDAO) {
this.userDAO = userDAO;
}
}java代码 数据访问层
public interface UserDAO {
public boolean validateUser(UserForm userForm) throws Exception;
}
public class UserDAOImpl extends BaseDAO implements UserDAO{ public boolean validateUser(UserForm userForm) throws Exception {
Session session = getSession();
Query query = session.createQuery("from User where uname=? and upass=?");
query.setParameter(0, userForm.getUname());
query.setParameter(1, userForm.getUpass());
List list = query.list();
if(null != list && list.size() > 0){
return true;
}
return false;
}
}java代码:获得session 就是这儿报错
public class BaseDAO {
private SessionFactory sf; private Session session;
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public Session getSession() {
return sf.getCurrentSession();//就是这儿报错
}
}请各位大哥帮忙看下

解决方案 »

  1.   

    <value>classpath:/com/ourcompany/dao/pojo</value>这里不要写classpath:直接<value>com/ourcompany/dao/pojo</value>
    ,private SessionFactory sf;不用提供setter,getter方法,你都配置了。
      

  2.   

    我在项目里自己建了个资源文件夹叫resource,我把applicationContext.xml放在里面的,编译出来的路么是在WEB-INF/classes/下面的,所以加classpath,应该是没有错的,<value>classpath:/com/ourcompany/dao/pojo</value>
    至于setter方法,我是通过spring在注入BaseDAO里面的SessionFactory,按照道理来说应该也是对的
      

  3.   

    各位大哥,再看下,根据报错信息好像说的是获得session时没有找到事务,
      

  4.   

    哈哈,我自己解决了,原来是我在applicationContext-services.xml里面定义了代理userBLLProxy,而在applicationContext-action.xml里面没有用,还是用的原来的userBLL,我说怎么一直报错提示没有在事务中,原来是这样。