Java和Spring.事务注释@Transactional [英] Java and Spring. Transactional annotation @Transactional

查看:86
本文介绍了Java和Spring.事务注释@Transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从DAO类中删除begin和commit事务,并且需要使用Transactional注释.应该怎么做? 现在,异常是org.hibernate.HibernateException:如果没有活动事务,createQuery无效

I want to Remove begin and commit transactions from DAO class , and I need to use Transactional annotation. How it should be done? Now , exception is org.hibernate.HibernateException: createQuery is not valid without active transaction

CoursesDAO.java

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

CoursesDAOImpl.java

@Repository
public class CoursesDAOImpl implements CoursesDAO {

 @Autowired
 private SessionFactory sessionFactory;

public Course createCourse(Course course) {

     Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    Integer id = (Integer) sessionFactory.getCurrentSession().save(course);
    course.setId(id);
    //session.getTransaction().commit();

    return course;
};

public Course findCourseById(Integer id) {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    Course course = (Course) session.get(Course.class, id);
    //session.getTransaction().commit();

    return course;
}


public Course updateCourse(Course course) {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    session.merge(course);
    //session.getTransaction().commit();

    return course;
};

public void deleteCourse(Course course) {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    session.delete(course);
    //session.getTransaction().commit();    



};

public List<Course> getAllCourses() {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    List listCourses = session.createQuery("from Course").list();
    //session.getTransaction().commit();
    return listCourses;
}



public List<Course> getAllCoursesByCategory(String category) {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    List listCoursesByCategory = session.createQuery("from Course c where c.category='"+category+"'").list();
    //session.getTransaction().commit();

    return listCoursesByCategory;
}


public List<Course> getAllCoursesWhichNoProposal( ) {

    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    List listCoursesNoProposal = session.createQuery("from Course c where c.state not like 'Proposal' and c.state not like 'Rejected'").list();
    //session.getTransaction().commit();

    return listCoursesNoProposal;
}

}

CourseService.java

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

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.openCurrentSessionwithTransaction();

    course = coursesDAOImpl.updateCourse(course);

    // Course.closeCurrentSessionwithTransaction();
    return course;
}
@Transactional
public Course createCourse(Course course) {

      //Course.openCurrentSessionwithTransaction();

    course = coursesDAOImpl.createCourse(course);

      //Course.closeCurrentSessionwithTransaction();
    return course;
}
@Transactional
public List<Course> getAllCourses() {

    // Course.openCurrentSessionwithTransaction();

    List<Course> listCourses = coursesDAOImpl.getAllCourses();

    // Course.closeCurrentSessionwithTransaction();
    return listCourses;
}
@Transactional
public List<Course> getAllCoursesByCategory(String category) {

    // Course.openCurrentSessionwithTransaction();

    List<Course> listCoursesByCategory = coursesDAOImpl
            .getAllCoursesByCategory(category);

    // Course.closeCurrentSessionwithTransaction();
    return listCoursesByCategory;
}

@Transactional
public List<Course> getAllCoursesWhichNoProposal( ) {

    // Course.openCurrentSessionwithTransaction();

    List<Course> listCoursesNoProposal = coursesDAOImpl
            .getAllCoursesWhichNoProposal( );

    // Course.closeCurrentSessionwithTransaction();
    return listCoursesNoProposal;
}
@Transactional
public void deleteCourse(Course course) {
    coursesDAOImpl.deleteCourse(course);

};

application-contex.xml

application-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="connection.pool_size">1</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>

推荐答案

我认为您在概念上有疑问.

I think here you have a problem of concept.

正确的体系结构是具有调用存储库的事务服务.像这样的东西.

The proper architecture would be to have a transactional service that invoke a repository. Something like this.

@Service
public class CoursesServiceImpl implements CoursesService {

  @Autowired
  private CoursesDAO coursesDAO;

  @Override
  @Transactional
  public void insertCourses(Course courses) {
    coursesDAO.createCourse(courses);
  }

}

然后您的存储库

@Repository
public class CoursesDAOImpl implements CoursesDAO {

@Autowired
private SessionFactory sessionFactory;

public Course createCourse(Course course) {

   Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    Integer id = (Integer) session.save(course);
    course.setId(id);
    //session.getTransaction().commit();

    return course;
};

这篇关于Java和Spring.事务注释@Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆