gpt4 book ai didi

Java 和 Spring。事务性注解@Transactional

转载 作者:行者123 更新时间:2023-12-01 11:23:39 24 4
gpt4 key购买 nike

我想从 DAO 类中删除开始和提交事务,并且我需要使用事务注释。应该怎么做呢? 现在,异常(exception)是 org.hibernate.HibernateException:createQuery 在没有 Activity 事务的情况下无效

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

}

类(class)DAOImpl.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

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.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

<?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>

最佳答案

我认为你在这里有一个概念问题。

正确的架构是拥有一个调用存储库的事务服务。像这样的东西。

@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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com