@Transactional(propagation = Propagation.REQUIRED)
public Users updateUser(Users user);
实施是:
@Override
public Users updateUser(Users user) {
Session session = this.sessionFactory.openSession();
Users updateUser = (Users) session.merge(user);
session.saveOrUpdate(updateUser);
return updateUser;
}
如果我使用session.beginTransaction().commit();
,则只会更新我的数据。
但我正在使用@Transactional,我知道维护会话和交易。任何人都可以告诉我为什么需要手动提交提交吗?
更新
调度-servlet.xml中
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--scan the given package for controllers-->
<context:component-scan base-package="com.edumongoose.controllers"/>
<context:component-scan base-package="com.edumongoose.compilers"/>
<!--support for @Controller and @RequestMapping-->
<mvc:annotation-driven />
<!--support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.-->
<context:annotation-config/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<!-- <bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />-->
</beans>
的applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--scan the given package for repository-->
<context:component-scan base-package="com.edumongoose.dao"/>
<context:component-scan base-package="com.edumongoose.daoimpl"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<!--tx annotation driven scans registered spring beans for @Transactional-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--support for @Controller and @RequestMapping-->
<mvc:annotation-driven/>
<!--support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.-->
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/contest" />
<property name="username" value="root" />
<property name="password" value=""/>
<property name="connection.characterEncoding" value = "utf-8"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.edumongoose.entity"/>
<property name="hibernateProperties">
<props>
<prop key="c3p0.minPoolSize">15</prop>
<prop key="c3p0.maxPoolSize">1000</prop>
<prop key="c3p0.timeout">5000</prop>
<prop key="c3p0.max_statement">500</prop>
<prop key="c3p0.testConnectionOnCheckout">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
15
</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/pagenotfound.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/jsp/500exception.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
Tiles.xml
<definition name="admintemplate" template="/WEB-INF/jsp/layout/admin/admintemplate.jsp">
<put-attribute name="admintop" value="/WEB-INF/jsp/layout/admin/admintop.jsp" />
<put-attribute name="adminleft" value="/WEB-INF/jsp/layout/admin/adminleft.jsp" />
<put-attribute name="adminbody" value="" />
<put-attribute name="adminbottom" value="/WEB-INF/jsp/layout/admin/adminbottom.jsp" />
</definition>
<definition name="landingtemplate" template="/WEB-INF/jsp/layout/landing/landingtemplate.jsp">
<put-attribute name="title" value="Contest | Home"/>
<put-attribute name="landingtop" value="/WEB-INF/jsp/layout/landing/landingtop.jsp" />
<put-attribute name="landingbody" value="" />
<put-attribute name="landingbottom" value="/WEB-INF/jsp/layout/landing/landingbottom.jsp" />
</definition>
<definition name="errorlayout" template="/WEB-INF/jsp/layout/errorlayout.jsp">
<put-attribute name="title" value="Page Not Found" type="string" />
<put-attribute name="errorbody" value="" />
</definition>
<definition name="home" extends="landingtemplate">
<put-attribute name="landingbody" value="/WEB-INF/jsp/homepage.jsp" />
</definition>
<definition name="signin" extends="landingtemplate">
<put-attribute name="landingbody" value="/WEB-INF/jsp/signin.jsp" />
</definition>
<definition name="signup" extends="landingtemplate">
<put-attribute name="landingbody" value="/WEB-INF/jsp/signup.jsp" />
</definition>
<definition name="dashboard" extends="usertemplate">
<put-attribute name="body" value="/WEB-INF/jsp/dashboard.jsp" />
</definition>
<definition name="solveproblem" extends="usertemplate">
<put-attribute name="body" value="/WEB-INF/jsp/solveproblem.jsp" />
</definition>
<definition name="listing" extends="usertemplate">
<put-attribute name="body" value="/WEB-INF/jsp/listing.jsp" />
</definition>
<definition name="admindashboard" extends="admintemplate">
<put-attribute name="adminbody" value="/WEB-INF/jsp/admindashboard.jsp" />
</definition>
<definition name="uploadproblem" extends="admintemplate">
<put-attribute name="adminbody" value="/WEB-INF/jsp/uploadproblem.jsp" />
</definition>
<definition name="problemlisting" extends="admintemplate">
<put-attribute name="adminbody" value="/WEB-INF/jsp/adminproblemlisting.jsp" />
</definition>