Java,Spring“saveOrUpdate”,DAO中的“更新”

时间:2015-06-26 19:56:36

标签: java spring hibernate

在Service类中执行了事务( @Transactional )后,DAO类工作中的命令“ saveOrUpdate ”,“更新”不会。没有错误,数据没有在数据库中更新

public class CoursesDAOImpl implements CoursesDAO {
public Course updateCourse(Course course) {
    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    session.update(course);
    //session.getTransaction().commit();
    return course;
};

@Service
public class CourseServiceImpl implements CourseService {
@Transactional
public Course updateCourse(Course course) {
     course = coursesDAOImpl.updateCourse(course);
    return course;
}

CoursesController.java

     public class CoursesController {
     @Autowired
     private CourseService courseService;
    @Autowired
     private UserCourseService userCourseService;
    @Autowired
    private UserService userService;
    @Autowired
     private CategoryService categoryService;
     @Autowired
     private ManagerCourseService managerCourseService;
     Course course = courseService.findCourseById(courseId);
        course.setName("sss");
        course.setDescription("ssss");

        course = courseService.updateCourse(course);
        return "redirect:/courses";
    }

应用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"     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:aop="http://www.springframework.org/schema/aop"
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/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<mvc:annotation-driven />
 <tx:annotation-driven transaction-manager="transactionManager"/>  
<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">false</prop> 
<prop key="hibernate.format_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>

<!--    <context:component-scan base-package="com.epam.edu.jtc" /> -->

 <context:component-scan base-package="com.epam.edu.jtc.dao" />
 <context:component-scan base-package="com.epam.edu.jtc.dto" />
 <context:component-scan base-package="com.epam.edu.jtc.entity" />
 <context:component-scan base-package="com.epam.edu.jtc.service" />
 <context:component-scan base-package="com.epam.edu.jtc.web" />
  <!--  <context:component-scan base-package="com.epam.edu.jtc.controller"> -->
  <!--     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
  <!-- </context:component-scan> -->
  </beans>

的web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--    <mvc:resources mapping="/resources/**" location="/resources/" /> -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:application-context.xml
    classpath:Spring-Mail.xml
  </param-value>
  </context-param>
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>TrainingCenterServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
    <servlet-name>TrainingCenterServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping> 
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
 </listener>
 </web-app>

1 个答案:

答案 0 :(得分:0)

通过添加Annotation based Transaction Management,确保在Spring配置文件中启用了<tx:annotation-driven/>。同时在MVC上下文中扫描与MVC相关的bean,例如:

<context:component-scan base-package="your.package.name.controller">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>