Permalink
Browse files

@Transactional

  • Loading branch information...
1 parent b429584 commit b96bca391858eaf613e8a8486d093227f4d347fc @alexec committed May 17, 2012
Showing with 28 additions and 1 deletion.
  1. +19 −0 src/main/java/tutorial/UsersController.java
  2. +9 −1 src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml
@@ -1,8 +1,10 @@
package tutorial;
import org.springframework.stereotype.Controller;
+import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@@ -23,4 +25,21 @@ public String users(Model model) {
return "users";
}
+
+ @RequestMapping(value = "/create-user", method = RequestMethod.GET)
+ public String createUser(Model model) {
+ return "create-user";
+ }
+
+ @RequestMapping(value = "/create-user", method = RequestMethod.POST)
+ @Transactional
+ public String createUser(Model model, String name) {
+
+ User user = new User();
+ user.setName(name);
+
+ entityManager.persist(user);
+
+ return "redirect:/users.html";
+ }
}
@@ -3,9 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
+ xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
+ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<context:component-scan base-package="tutorial"/>
@@ -18,8 +20,14 @@
<jee:jndi-lookup id="tutorialDS" jndi-name="java:/comp/env/jdbc/tutorialDS" expected-type="javax.sql.DataSource"/>
- <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="tutorialDS"/>
</bean>
+ <tx:annotation-driven/>
+
+ <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ </bean>
+
</beans>

0 comments on commit b96bca3

Please sign in to comment.