em.getTransaction().begin();
MessageListenerImpl.resetCounters();
// Create and persist a new Message entity
Message message = new Message("Hello Persistence!");
assertNull("Test message's created field to be null.", message.getCreated());
assertNull("Test message's updated field to be null.", message.getUpdated());
em.persist(message);
// Pre-persist invoked, created and updated fields set
assertStatus(1, 0, 0, 0, 0, 0, 0);
assertNotNull("Test message's created field being set.", message.getCreated());
assertNotNull("Test message's updated field being set.", message.getUpdated());
// Perform a simple query to get the Message
Query q = em.createQuery("select m from Message m where m.id=" + message.getId());
Message m = (Message) q.getSingleResult();
assertEquals("Test first expected message.", "Hello Persistence!",m.getMessage());
assertNotNull("Test message's created field being set.", m.getCreated());
assertNotNull("Test message's updated field being set.", m.getUpdated());
// Query cause flush to occur, hence fire the postPersist event
assertStatus(1, 1, 0, 0, 0, 0, 0);
// Create and persist another new Message entity
message = new Message("Hello Persistence 2!");
assertNull("Test message's created field to be null.", message.getCreated());
assertNull("Test message's updated field to be null.", message.getUpdated());
em.persist(message);