我需要使用Transactional注释。怎么做? 现在,异常是org.hibernate.HibernateException:没有活动事务,createQuery无效。 如果我删除线程,那将是异常 - 找不到当前线程的会话。 我还应该做什么?
CoursesDAO.java
public interface CoursesDAO {
public Course createCourse(Course course);
public Course findCourseById(Integer key);
public Course updateCourse(Course course);
public void deleteCourse(Course course);
public List<Course> getAllCourses();
public List<Course> getAllCoursesByCategory(String category);
public List<Course> getAllCoursesWhichNoProposal( );
}
CoursesDAOImpl.java
@Repository
public class CoursesDAOImpl implements CoursesDAO {
@Autowired
private SessionFactory sessionFactory;
public Course createCourse(Course course) {
Session session = sessionFactory.getCurrentSession();
Integer id = (Integer) sessionFactory.getCurrentSession().save(course);
course.setId(id);
return course;
};
public Course findCourseById(Integer id) {
Session session = sessionFactory.getCurrentSession();
Course course = (Course) session.get(Course.class, id);
return course;
}
public Course updateCourse(Course course) {
Session session = sessionFactory.getCurrentSession();
session.merge(course);
return course;
};
public void deleteCourse(Course course) {
Session session = sessionFactory.getCurrentSession();
session.delete(course);
};
public List<Course> getAllCourses() {
Session session = sessionFactory.getCurrentSession();
List listCourses = session.createQuery("from Course").list();
return listCourses;
}
public List<Course> getAllCoursesByCategory(String category) {
Session session = sessionFactory.getCurrentSession();
List listCoursesByCategory = session.createQuery("from Course c where c.category='"+category+"'").list();
return listCoursesByCategory;
}
public List<Course> getAllCoursesWhichNoProposal( ) {
Session session = sessionFactory.getCurrentSession();
List listCoursesNoProposal = session.createQuery("from Course c where c.state not like 'Proposal' and c.state not like 'Rejected'").list();
return listCoursesNoProposal;
}
}
CourseService.java
public interface CourseService {
public Course findCourseById(Integer id);
public Course updateCourse(Course course);
public Course createCourse(Course course);
public void deleteCourse(Course course);
public List<Course> getAllCourses();
public List<Course> getAllCoursesByCategory(String category);
public List<Course> getAllCoursesWhichNoProposal( );
}
CourseServiceImpl.java
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CoursesDAO coursesDAOImpl;
@Transactional
public Course findCourseById(Integer id) {
Course course = coursesDAOImpl.findCourseById(id);
return course;
}
@Transactional
public Course updateCourse(Course course) {
course = coursesDAOImpl.updateCourse(course);
return course;
}
@Transactional
public Course createCourse(Course course) {
course = coursesDAOImpl.createCourse(course);
return course;
}
@Transactional
public List<Course> getAllCourses() {
List<Course> listCourses = coursesDAOImpl.getAllCourses();
return listCourses;
}
@Transactional
public List<Course> getAllCoursesByCategory(String category) {
List<Course> listCoursesByCategory = coursesDAOImpl
.getAllCoursesByCategory(category);
return listCoursesByCategory;
}
@Transactional
public List<Course> getAllCoursesWhichNoProposal( ) {
List<Course> listCoursesNoProposal = coursesDAOImpl
.getAllCoursesWhichNoProposal( );
return listCoursesNoProposal;
}
@Transactional
public void deleteCourse(Course course) {
coursesDAOImpl.deleteCourse(course);
};
应用contex.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:security="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<mvc:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:tcp://localhost:9092/~/QWE;INIT=create schema if not exists QWE\;"
p:username="sa"
p:password="" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.default_schema">QWE</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.epam.edu.jtc.entity.User</value>
<value>com.epam.edu.jtc.entity.Category</value>
<value>com.epam.edu.jtc.entity.Course</value>
<value>com.epam.edu.jtc.entity.UserCourse</value>
<value>com.epam.edu.jtc.entity.ManagerCourse</value>
</list>
</property>
</bean>
<!-- FreeMarker Configuration -->
<bean id="freemarkerEmailConfig" class="freemarker.template.Configuration">
<property name="directoryForTemplateLoading" value="WEB-INF/pages/templates" />
<property name="objectWrapper">
<bean class="freemarker.template.DefaultObjectWrapper"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.epam.edu.jtc" />
</beans>
答案 0 :(得分:0)
根据我的理解,问题在于你处理会话的方式
getCurrentSession():获取当前会话。
openSession():创建一个新会话
除非您有充分的理由,否则不建议使用以下链接
<prop key="hibernate.current_session_context_class">thread</prop>