Zapraszamy na płatny, wakacyjny staż TouK!
Zgłoszenia przyjmujemy do 17.05.
@Test public void savingAndUpdatingPersonShouldCreateTwoHistoricalVersions() { //given Person person = createAndSavePerson(); String oldFirstName = person.getFirstName(); String newFirstName = oldFirstName + "NEW"; //when updatePersonWithNewName(person, newFirstName); //then verifyTwoHistoricalVersionsWereSaved(oldFirstName, newFirstName); } private Person createAndSavePerson() { Transaction transaction = session.beginTransaction(); Person person = PersonFactory.createPerson(); session.save(person); transaction.commit(); return person; } private void updatePersonWithNewName(Person person, String newName) { Transaction transaction = session.beginTransaction(); person.setFirstName(newName); session.update(person); transaction.commit(); } private void verifyTwoHistoricalVersionsWereSaved(String oldFirstName, String newFirstName) { List<Object[]> personRevisions = getPersonRevisions(); assertEquals(2, personRevisions.size()); assertEquals(oldFirstName, ((Person)personRevisions.get(0)[0]).getFirstName()); assertEquals(newFirstName, ((Person)personRevisions.get(1)[0]).getFirstName()); } private List<Object[]> getPersonRevisions() { Transaction transaction = session.beginTransaction(); AuditReader auditReader = AuditReaderFactory.get(session); List<Object[]> personRevisions = auditReader.createQuery() .forRevisionsOfEntity(Person.class, false, true) .getResultList(); transaction.commit(); return personRevisions; }
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > ... <property name="eventListeners"> <map key-type="java.lang.String" value-type="org.hibernate.event.EventListeners"> <entry key="post-insert" value-ref="auditEventListener"/> <entry key="post-update" value-ref="auditEventListener"/> <entry key="post-delete" value-ref="auditEventListener"/> <entry key="pre-collection-update" value-ref="auditEventListener"/> <entry key="pre-collection-remove" value-ref="auditEventListener"/> <entry key="post-collection-recreate" value-ref="auditEventListener"/> </map> </property> </bean> <bean id="auditEventListener" class="org.hibernate.envers.event.AuditEventListener" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource"/> </bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"/> </bean>