写了一个登陆,发现一个问题,是在不知道怎么解决了,求各位大哥大姐帮帮忙,万分感谢!
事情经过是这样的:
我在dao使用getCurrentSession()来获取session,但是在运行的时候提示:No CurrentSessionContext configured!
然后百度知道需要加:<prop key="hibernate.current_session_context_class">thread</prop>配置
于是我把这个配置加到hibernate property中,再次运行项目。
接着又发现:createQuery is not valid without active transaction
于是又百度了一下,有人说getCurrentSession()不是事物的那个session还是怎么回事,我水平比较低,也没看太懂
他说去掉:<prop key="hibernate.current_session_context_class">thread</prop>
但是去掉的话又会报第一个错误,有没有人帮忙看看这是怎么回事,谢了!
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- spring -->
<context:component-scan base-package="foo.services"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/propertyfile/jdbc.properties</value>
<value>WEB-INF/propertyfile/hibernate.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="driverClass" value="${c3p0.driverClass}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="foo.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="move*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDao" class="foo.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
dao:
public boolean findlogin(User user){
Session session = sessionFactory.getCurrentSession();
try{
String sqlQuery = "FROM User WHERE user=? AND pass=?";
Query query = session.createQuery(sqlQuery);
query.setParameter(1, user.getUser());
query.setParameter(2, user.getPass());
List loginInfo = query.list();
log.info(user.getUser()+"login succes");
System.out.println(loginInfo.size());
if(loginInfo.size()==0){
return true;
}
}catch(RuntimeException run){
run.printStackTrace();
log.info(user.getUser()+"login error");
return false;
}
return false;
}
------解决方案--------------------