我在使用Hibernate4时有一个spring应用程序,它与StageDaoImpl中的sessionFactory.openSession()一起使用:
@Repository(value = "stageDao")
public class StageDaoImpl implements StageDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public Integer editStage(Stage stage) {
Session session = sessionFactory.openSession();
Integer ban = 1;
try {
session.beginTransaction();
session.update(stage);
session.getTransaction().commit();
} catch (HibernateException e) {
ban = -1;
} catch (Exception e) {
ban = -2;
} finally {
session.close();
}
return ban;
}
我尝试将editStage更改为@Transactional注释,但我不知道如何配置事务,它始终是相同的结果
@Repository(value = "stageDao")
public class StageDaoImpl implements StageDao {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public Integer editStage(Stage stage) {
Session session = sessionFactory.getCurrentSession();
Integer ban = 1;
try {
session.update(stage);
} catch (HibernateException e) {
ban = -1;
} catch (Exception e) {
ban = -2;
}
return ban;
}
错误StackTrace:
org.hibernate.HibernateException: No Session found for current thread
服务队-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- holding properties for database connectivity / -->
<context:property-placeholder location="classpath:/config/jdbc.properties" ignore-unresolvable="true" order="1" />
<!-- enabling annotation driven configuration / -->
<context:annotation-config />
<!-- Sesion factory -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<property name="packagesToScan" value="org.school.graduation.entities"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.InformixDialect</prop>
<prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>
</props>
</property>
</bean>
<!-- The only thing in here is the driver for hibernate-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/ by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<beans:bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<beans:property name="order" value="1" />
<beans:property name="basename" value="views"/>
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="order" value="1" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure the multipart resolver -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="50485760" />
</beans:bean>
<context:component-scan base-package="org.school.graduation" />
<task:annotation-driven />
</beans:beans>
根context.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.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<import resource="classpath:**/*-config.xml"/>
</beans>
StageStepServiceImpl.java
@Component
@Service("stageStepService")
public class StageStepServiceImpl implements StageStepService {
@Resource(name="stageDao")
private StageDao stageDao;
@Override
public Integer generatingStage( Stage stage ) {
Integer serviceValue = 0;
try {
serviceValue = stageDao.editStage(stage);
} catch (Exception e) {
serviceValue = -5;
}
return serviceValue;
}
}
它是一个Maven多模块项目
graduation-dao
├── src/main/java
│ ├── org.school.graduation.dao.administrator.provider
│ │ └── StageDao.java
│ └── org.school.graduation.dao.administrator.providerImpl
│ └── StageDaoImpl.java
├── Maven Dependencies
├── JRE System Library
├── src
├── target
└── pom.xml
graduation-services
├── src/main/java
│ ├── org.school.graduation.services.administrator.provider
│ │ └── StageService.java
│ └── org.school.graduation.services.administrator.providerImpl
│ └── StageServiceImpl.java
├── Maven Dependencies
├── JRE System Library
├── src
├── target
└── pom.xml
graduation-web
├── src/main/java
│ └── org.school.graduation.controller.administrator.provider
│ └── StageController.java
├── src/main/resources
│ ├── root-context.mxl
│ ├── services-config.mxl
│ └── servlet-context.mxl
├── Maven Dependencies
├── JRE System Library
├── src
├── target
└── pom.xml