title: Using spring + jpa + hibernate but can't persist entity and never throws exception , tests passed
body: <p>I've been testing for a long time and searching for hours. Since there' re some similar questions, but after trying, it still doesn't work , I am using spring MVC and jpa with hibernate to set up a base project, but the problem is I cannot persist entity into database when I put it into jetty. What's strange is that the Junit tests are passed.</p>
  
  <p>Here is the essential code.</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;controller&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/controller.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;controller&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>db-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
      &lt;!-- DBCP DataSource --&gt;
      &lt;bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/spring" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- JPA EntityManagerFactory --&gt;
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean id="jpaVendorAdapter"
                  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="generateDdl" value="true" /&gt;
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Persistence Context Annotated --&gt;
      &lt;bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
          id="persistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;!-- Transaction --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>persistence.xml(in META-INF folder)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0"&gt;
  
      &lt;persistence-unit name="persist" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="create-drop" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
              &lt;property name="hibernate.transaction.flush_before_completion"
                  value="true" /&gt;
              &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Person.java (entity bean)</p>
  
  <pre><code>@Entity
  public class Person {
  
      private int id;
      private String name;
  
      @Id
      @GeneratedValue
      public int getId() {
          return id;
      }
  
      // ..
  
  }
  </code></pre>
  
  <p>PersonDaoImpl.java</p>
  
  <pre><code>@Repository
  @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
  public class PersonDaoImpl implements PersonDao {
  
      private EntityManager entityManager;
  
      @PersistenceContext
      public void setEntityManager(EntityManager entityManager) {
          this.entityManager = entityManager;
      }
  
      @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
      public Person persist(Person person) {
          entityManager.persist(person);
          return person;
      }
  
      // ...
  
  }
  </code></pre>
  
  <p>PersonServiceImpl.java</p>
  
  <pre><code>@Service
  public class PersonServiceImpl implements PersonService {
  
      private PersonDao personDao;
  
      @Inject
      public void setPersonDao(PersonDao personDao) {
          this.personDao = personDao;
      }
  
      public Person create(Person person) {
          return personDao.persist(person);
      }
  
      // @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
      public Person save(Person person) {
          return personDao.merge(person);
      }
  
      // ...
  
  }
  </code></pre>
  
  <p>PersonController.java</p>
  
  <pre><code>@Controller
  @Scope("prototype")
  @RequestMapping("/person")
  public class PersonController {
  
      private PersonService personService;
  
      @Inject
      public void setPersonService(PersonService personService) {
          this.personService = personService;
      }
  
      @RequestMapping(value = "/add", method = RequestMethod.POST)
      public ModelAndView add(@RequestParam("username") String name) {
          Person person = new Person();
          person.setName(name);
          System.out.println(name);  // i can get the value
          personService.create(person); // nothing happend... throws no Excp
          ModelAndView mav = new ModelAndView();
          mav.setViewName("result");
          mav.addObject("val", person.getName());
          return mav;
      }
  
  }
  </code></pre>
  
  <p>PersonControllerTest.java</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/test-context.xml")
  public class PersonControllerTest {
  
      private PersonController controller;
  
      @Inject
      public void setController(PersonController controller) {
          this.controller = controller;
      }
  
  
          // the test passed no matter @Transactional or not, if not, the db get the row
      @Test(/*expected = NullPointerException.class*/)
      @Transactional
      public void testAdd() {
  //      PersonController controller = new PersonController();
          assertEquals("test", controller.add("test").getModelMap().get("val"));
      }
  
  }
  </code></pre>
  
  <p>Some xml like context and mvc config xml and import declaration are omitted, the servie and dao interface, can anyone help me ? </p>