public static List<Trade> getOldVersionList(Trade trade) { Session session = HibernateUtil.getSession(); List<Trade> result = new ArrayList<>(); try { if (trade != null) { result.add(trade); AuditReader reader = AuditReaderFactory.get(session); List<Object[]> prior_revision = (List<Object[]>) reader .createQuery() .forRevisionsOfEntity(trade.getClass(), false, true) .add(AuditEntity.property("tradeVersion").lt(trade.getTradeVersion())) .add(AuditEntity.id().eq(trade.getId())) .addOrder(AuditEntity.revisionNumber().desc()) .getResultList(); for (Object[] objects : prior_revision) { Trade version = (Trade) objects[0]; unProxyObject(version); result.add(version); } } } catch (ClassNotFoundException | IllegalAccessException | HibernateException | IllegalArgumentException | InvocationTargetException e) { logger.error(StringUtils.formatErrorMessage(e)); } finally { session.close(); } return result; }
@Test public void testAuditQueryUsingEmbeddableNotEquals() { final NameInfo nameInfo = new NameInfo("Jane", "Doe"); final AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(Person.class, 1); query.add(AuditEntity.property("nameInfo").ne(nameInfo)); assertEquals(0, query.getResultList().size()); }
@Test public void testAuditQueryUsingEmbeddableEquals() { final NameInfo nameInfo = new NameInfo("John", "Doe"); final AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(Person.class, 1); query.add(AuditEntity.property("nameInfo").eq(nameInfo)); List<?> results = query.getResultList(); assertEquals(1, results.size()); final Person person = (Person) results.get(0); assertEquals(nameInfo, person.getNameInfo()); }
@Test public void testAuditQueryUsingEmbeddableNonEqualityCheck() { try { final NameInfo nameInfo = new NameInfo("John", "Doe"); final AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(Person.class, 1); query.add(AuditEntity.property("nameInfo").le(nameInfo)); } catch (Exception ex) { assertTyping(AuditException.class, ex); } }
@Override public GeoObject getByRevision(Long id, Integer revId) { AuditQuery query = AuditReaderFactory.get(getCurrentSession()) .createQuery() .forEntitiesAtRevision(GeoObject.class, revId); query.add(AuditEntity.id().eq(id)); GeoObject object = (GeoObject) query.getSingleResult(); AuditQuery queryTags = AuditReaderFactory.get(getCurrentSession()) .createQuery() .forEntitiesAtRevision(GeoObjectTag.class, revId); // queryTags.add(AuditEntity.property("geoObject").eq(id)); queryTags.add(AuditEntity.property("geoObject").eq(object)); List tags = queryTags.getResultList(); object.setTags(new HashSet<GeoObjectTag>(tags)); return object; }
@Override public List<Object[]> getHistoryList(Long id) { AuditQueryCreator query = AuditReaderFactory.get(getCurrentSession()).createQuery(); List<Object[]> resultList = query .forRevisionsOfEntity(GeoObject.class, false, false) .add(AuditEntity.id().eq(id)) .getResultList(); Collections.reverse(resultList); return resultList; }
@Test public void testHasChanged() throws Exception { List list = queryForPropertyHasChangedWithDeleted(BasicTestEntity1.class, id1, "str1"); assertEquals(1, list.size()); assertEquals(makeList(1), extractRevisionNumbers(list)); list = queryForPropertyHasChangedWithDeleted(BasicTestEntity1.class, id1, "long1"); assertEquals(1, list.size()); assertEquals(makeList(1), extractRevisionNumbers(list)); list = getAuditReader() .createQuery() .forRevisionsOfEntity(BasicTestEntity1.class, false, true) .add(AuditEntity.property("str1").hasChanged()) .add(AuditEntity.property("long1").hasChanged()) .getResultList(); assertEquals(1, list.size()); assertEquals(makeList(1), extractRevisionNumbers(list)); }
@Test public void testHasChangedCar1() throws Exception { List list = getAuditReader() .createQuery() .forRevisionsOfEntity(Car.class, false, false) .add(AuditEntity.id().eq(id_car1)) .add(AuditEntity.property("owners").hasChanged()) .getResultList(); assertEquals(1, list.size()); assertEquals(makeList(1), extractRevisionNumbers(list)); list = getAuditReader() .createQuery() .forRevisionsOfEntity(Car.class, false, false) .add(AuditEntity.id().eq(id_car1)) .add(AuditEntity.property("owners").hasNotChanged()) .getResultList(); assertEquals(0, list.size()); }
@Test public void testHasChangedPerson2() throws Exception { List list = getAuditReader() .createQuery() .forRevisionsOfEntity(Person.class, "Personaje", false, false) .add(AuditEntity.id().eq(id_pers2)) .add(AuditEntity.property("cars").hasChanged()) .getResultList(); assertEquals(2, list.size()); assertEquals(makeList(1, 2), extractRevisionNumbers(list)); list = getAuditReader() .createQuery() .forRevisionsOfEntity(Person.class, "Personaje", false, false) .add(AuditEntity.id().eq(id_pers2)) .add(AuditEntity.property("cars").hasNotChanged()) .getResultList(); assertEquals(0, list.size()); }
@Test public void testRevisionTypes() { @SuppressWarnings({"unchecked"}) List<Object[]> results = getAuditReader() .createQuery() .forRevisionsOfEntity(StrTestEntity.class, false, true) .add(AuditEntity.id().eq(id)) .getResultList(); assertEquals(results.get(0)[2], RevisionType.ADD); assertEquals(results.get(1)[2], RevisionType.MOD); }