Search in sources :

Example 1 with RevisionType

use of org.hibernate.envers.RevisionType in project hibernate-orm by hibernate.

the class BaseEnversCollectionEventListener method generateFakeBidirecationalRelationWorkUnits.

private void generateFakeBidirecationalRelationWorkUnits(AuditProcess auditProcess, PersistentCollection newColl, Serializable oldColl, String collectionEntityName, String referencingPropertyName, AbstractCollectionEvent event, RelationDescription rd) {
    // First computing the relation changes
    final List<PersistentCollectionChangeData> collectionChanges = getEnversService().getEntitiesConfigurations().get(collectionEntityName).getPropertyMapper().mapCollectionChanges(event.getSession(), referencingPropertyName, newColl, oldColl, event.getAffectedOwnerIdOrNull());
    // Getting the id mapper for the related entity, as the work units generated will correspond to the related
    // entities.
    final String relatedEntityName = rd.getToEntityName();
    final IdMapper relatedIdMapper = getEnversService().getEntitiesConfigurations().get(relatedEntityName).getIdMapper();
    // For each collection change, generating the bidirectional work unit.
    for (PersistentCollectionChangeData changeData : collectionChanges) {
        final Object relatedObj = changeData.getChangedElement();
        final Serializable relatedId = (Serializable) relatedIdMapper.mapToIdFromEntity(relatedObj);
        final RevisionType revType = (RevisionType) changeData.getData().get(getEnversService().getAuditEntitiesConfiguration().getRevisionTypePropName());
        // This can be different from relatedEntityName, in case of inheritance (the real entity may be a subclass
        // of relatedEntityName).
        final String realRelatedEntityName = event.getSession().bestGuessEntityName(relatedObj);
        // By default, the nested work unit is a collection change work unit.
        final AuditWorkUnit nestedWorkUnit = new CollectionChangeWorkUnit(event.getSession(), realRelatedEntityName, rd.getMappedByPropertyName(), getEnversService(), relatedId, relatedObj);
        auditProcess.addWorkUnit(new FakeBidirectionalRelationWorkUnit(event.getSession(), realRelatedEntityName, getEnversService(), relatedId, referencingPropertyName, event.getAffectedOwnerOrNull(), rd, revType, changeData.getChangedElementIndex(), nestedWorkUnit));
    }
    // We also have to generate a collection change work unit for the owning entity.
    auditProcess.addWorkUnit(new CollectionChangeWorkUnit(event.getSession(), collectionEntityName, referencingPropertyName, getEnversService(), event.getAffectedOwnerIdOrNull(), event.getAffectedOwnerOrNull()));
}
Also used : Serializable(java.io.Serializable) RevisionType(org.hibernate.envers.RevisionType) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AuditWorkUnit(org.hibernate.envers.internal.synchronization.work.AuditWorkUnit) FakeBidirectionalRelationWorkUnit(org.hibernate.envers.internal.synchronization.work.FakeBidirectionalRelationWorkUnit) PersistentCollectionChangeData(org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData) CollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit) PersistentCollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)

Example 2 with RevisionType

use of org.hibernate.envers.RevisionType in project hibernate-orm by hibernate.

the class DetachedMultipleCollectionChangeTest method getAuditJoinTableRows.

private List<AuditJoinTableInfo> getAuditJoinTableRows(String middleEntityName, String joinColumnIdName, String joinColumnIdProp, String inverseJoinColumnIdName, String inverseJoinColumnIdProp, String revProp, String revIdProp, String revTypeProp) throws Exception {
    StringBuilder qryBuilder = new StringBuilder("select ");
    qryBuilder.append("aud ");
    qryBuilder.append(", ").append(joinColumnIdProp).append(" as joinColumnId");
    qryBuilder.append(", ").append(inverseJoinColumnIdProp).append(" as inverseJoinColumnId");
    qryBuilder.append(", ").append(revProp).append(" as rev");
    qryBuilder.append(", ").append(revIdProp).append(" as revId");
    qryBuilder.append(", ").append(revTypeProp).append(" as revType");
    qryBuilder.append(" from ").append(middleEntityName).append(" aud ");
    qryBuilder.append(" order by joinColumnId asc, inverseJoinColumnId asc, revId asc");
    String query = qryBuilder.toString();
    EntityManager em = createIsolatedEntityManager();
    Query qry = em.createQuery(query);
    @SuppressWarnings("unchecked") List<Object[]> auditJoinTableRows = qry.getResultList();
    List<AuditJoinTableInfo> result = new ArrayList<AuditJoinTableInfo>(auditJoinTableRows.size());
    for (Object[] auditJoinTableRow : auditJoinTableRows) {
        Long joinColumnId = (Long) auditJoinTableRow[1];
        Long inverseJoinColumnId = (Long) auditJoinTableRow[2];
        SequenceIdRevisionEntity rev = (SequenceIdRevisionEntity) auditJoinTableRow[3];
        RevisionType revType = (RevisionType) auditJoinTableRow[5];
        AuditJoinTableInfo info = new AuditJoinTableInfo(middleEntityName, rev, revType, joinColumnIdName, joinColumnId, inverseJoinColumnIdName, inverseJoinColumnId);
        result.add(info);
    }
    return result;
}
Also used : Query(javax.persistence.Query) SequenceIdRevisionEntity(org.hibernate.envers.enhanced.SequenceIdRevisionEntity) ArrayList(java.util.ArrayList) EntityManager(javax.persistence.EntityManager) RevisionType(org.hibernate.envers.RevisionType)

Example 3 with RevisionType

use of org.hibernate.envers.RevisionType in project hibernate-orm by hibernate.

the class DefaultTrackingEntitiesTest method testTrackModifiedEntitiesGroupByRevisionType.

@Test
public void testTrackModifiedEntitiesGroupByRevisionType() {
    StrIntTestEntity site = new StrIntTestEntity("y", 2, siteId);
    Map<RevisionType, List<Object>> result = getCrossTypeRevisionChangesReader().findEntitiesGroupByRevisionType(2);
    assert TestTools.checkCollection(result.get(RevisionType.ADD));
    assert TestTools.checkCollection(result.get(RevisionType.MOD), site);
    assert TestTools.checkCollection(result.get(RevisionType.DEL));
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) RevisionType(org.hibernate.envers.RevisionType) List(java.util.List) Test(org.junit.Test)

Example 4 with RevisionType

use of org.hibernate.envers.RevisionType in project hibernate-orm by hibernate.

the class DefaultTrackingEntitiesTest method testTrackDeletedEntitiesGroupByRevisionType.

@Test
public void testTrackDeletedEntitiesGroupByRevisionType() {
    StrTestEntity ste = new StrTestEntity(null, steId);
    StrIntTestEntity site = new StrIntTestEntity(null, null, siteId);
    Map<RevisionType, List<Object>> result = getCrossTypeRevisionChangesReader().findEntitiesGroupByRevisionType(3);
    assert TestTools.checkCollection(result.get(RevisionType.ADD));
    assert TestTools.checkCollection(result.get(RevisionType.MOD));
    assert TestTools.checkCollection(result.get(RevisionType.DEL), site, ste);
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) StrTestEntity(org.hibernate.envers.test.entities.StrTestEntity) RevisionType(org.hibernate.envers.RevisionType) List(java.util.List) Test(org.junit.Test)

Example 5 with RevisionType

use of org.hibernate.envers.RevisionType in project hibernate-orm by hibernate.

the class SimpleQuery method testEntitiesAddedAtRevision.

@Test
public void testEntitiesAddedAtRevision() {
    StrIntTestEntity site1 = new StrIntTestEntity("a", 10, id1);
    StrIntTestEntity site2 = new StrIntTestEntity("a", 10, id2);
    StrIntTestEntity site3 = new StrIntTestEntity("b", 5, id3);
    List result = getAuditReader().createQuery().forEntitiesModifiedAtRevision(StrIntTestEntity.class, StrIntTestEntity.class.getName(), 1).getResultList();
    RevisionType revisionType = (RevisionType) getAuditReader().createQuery().forEntitiesModifiedAtRevision(StrIntTestEntity.class, 1).addProjection(AuditEntity.revisionType()).add(AuditEntity.id().eq(id1)).getSingleResult();
    Assert.assertTrue(TestTools.checkCollection(result, site1, site2, site3));
    Assert.assertEquals(revisionType, RevisionType.ADD);
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) RevisionType(org.hibernate.envers.RevisionType) List(java.util.List) Test(org.junit.Test)

Aggregations

RevisionType (org.hibernate.envers.RevisionType)10 List (java.util.List)7 StrIntTestEntity (org.hibernate.envers.test.entities.StrIntTestEntity)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)2 Serializable (java.io.Serializable)1 Map (java.util.Map)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 SequenceIdRevisionEntity (org.hibernate.envers.enhanced.SequenceIdRevisionEntity)1 RevisionTypeType (org.hibernate.envers.internal.entities.RevisionTypeType)1 PersistentCollectionChangeData (org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 AuditWorkUnit (org.hibernate.envers.internal.synchronization.work.AuditWorkUnit)1 CollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)1 FakeBidirectionalRelationWorkUnit (org.hibernate.envers.internal.synchronization.work.FakeBidirectionalRelationWorkUnit)1 PersistentCollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)1 RevisionTypeAuditExpression (org.hibernate.envers.query.criteria.internal.RevisionTypeAuditExpression)1