title: Hibernate listeners are not getting invoked in Spring JUnit test
body: <p>I'm creating Spring MVC application with Hibernate as the data access layer. All my entities contain <code>createdOn</code> and <code>updatedOn</code> fields, which contain timestamps when the entity was created/updated. The <code>createdOn</code> field is set using <code>PreInsertEventListener</code> and the <code>updatedOn</code> field is set using <code>PreUpdateEventListener</code>.</p>
  
  <p>When I run my application, it works as expected, listeners are registered and when I save an entity, appropriate listeners are invoked (verified using debugger). But when I try to unit test this behavior, the listeners are registered, but never invoked.</p>
  
  <p>I determined using debugging that when I save an entity in my application <code>EntityInsertAction#preInsert</code> is invoked, which loops through the registered listeners and invokes them. But when I run my unit test this method is never invoked.</p>
  
  <p>Here is an example of my unit test:</p>
  
  <pre><code>@ActiveProfiles("test")
  @Transactional
  @TransactionConfiguration
  @ContextConfiguration(classes = {
          BaseConfiguration.class,
          DatabaseConfiguration.class
  })
  @RunWith(SpringJUnit4ClassRunner.class)
  public class HibernateListenerTest {
  
      @Autowired
      private FooDao fooDao;
  
      @Test
      public void testPreUpdateListener() {
          Foo foo = fooDao.merge(createFoo());
          Assert.assertNotNull(foo.getCreatedOn());
          Assert.assertNull(foo.getUpdatedOn());
  
          foo.setBar("Bar");
          foo = fooDao.merge(foo);
          Assert.assertNotNull(foo.getUpdatedOn());
      }
  
  }
  </code></pre>
  
  <p>Because the listeners are not invoked the unit tests always fail with the following exception:</p>
  
  <p><code>org.hibernate.PropertyValueException: not-null property references a null or transient value</code></p>
  
  <p>So my question is: why is this behavior different between running the application and running the unit test and if there is a way to fix the code so that the listeners are correctly invoked in my unit tests?</p>
  
  <p>Thanks in advance for any input. Please leave a comment if additional code/information is needed.</p>