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()));
}
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;
}
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));
}
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);
}
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);
}
Aggregations