title: JPA merge not working in ApplicationListener but working in Controller
body: <p>I'm having some weird problem with my JPA merge function. I am trying to get the last login times of my users and I want to update the user in the database when they login.</p>
  
  <p>The application listener for successful login events:</p>
  
  <pre><code>@Component
  public class SuccessLoginApplicationListener implements ApplicationListener&lt;AuthenticationSuccessEvent&gt;{
      @Autowired
      private UserDao userDao;
  
      @Override
      public void onApplicationEvent(AuthenticationSuccessEvent loginEvent) {
          User user = (User) loginEvent.getAuthentication().getPrincipal();
          user.setLastLogin(100000);
          userDao.merge(user);
      }
  }
  </code></pre>
  
  <p>UserDao</p>
  
  <pre><code>@Transactional
  @Repository("userDao")
  public class UserDao{
      protected EntityManager em;
  
      @PersistenceContext
      public void setEntityManager(EntityManager em) {
          this.em = em;
      }
  
      public User merge(User user){
          return em.merge(user);
      }
  }
  </code></pre>
  
  <p>Because the actual code includes a bunch of interfaces, other methods etc., the above code should include all that is important relevant to this question, I hope I did not forget something.</p>
  
  <p>When I do the following in a controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/profile")
  public class ProfileController {
      @Autowired
      private UserDao userDao;
  
      @RequestMapping(value="", method=RequestMethod.GET)
      public String profile(Model model){
          User user = getCurrentlyLoggedInUser();
          user.setLastLogin(1000000);
          userDao.merge(user);
          return "account/profile";
      }
  }
  </code></pre>
  
  <p>Everything works just as I would expect it to work, the database gets successfully changed to the new lastLogin value.</p>
  
  <p>The problem is when a successful login happens, even tho I do exactly the same as in the controller, the database does <strong>not</strong> get updates... I have confirmed that the methods are actually called (<code>onApplicationEvent</code>, <code>merge</code>).</p>
  
  <p>I am absolutely clueless why it <strong>doesn't</strong> work in the <code>ApplicationListener</code>, but it <strong>does</strong> work in the controller. Am I not understanding something about the merge function properly? </p>
  
  <p>I read the following: When you call merge, a managed entity is returned and changes to the managed entity are supposedly updated in the database, so I've also tried the following in the <code>userDao</code>:</p>
  
  <pre><code>public User merge(User user){
      User user = em.merge(user);
      user.setLastLogin(100000);
  }
  </code></pre>
  
  <p>The same thing happens here, when the merge function is called from the ApplicationListener, nothing is changed in the database, but when it is called from the Controller, the field is correctly set to 100000.</p>
  
  <p>Thanks for reading, hope you can help! :)</p>