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 of unLazyObjectRecursively method, of class AuditAccessor. */ @Test public void testUnLazyObjectRecursively_Object_int() throws Exception { System.out.println("unLazyObjectRecursively"); Trade trade = (Trade) HibernateUtil.getObjectWithQuery( "from Trade t where t.tradeId in (select min(tradeId) from Trade)"); AuditAccessor.unLazyObjectRecursively(trade, 5); assertNotNull("failed lazy loading trade", trade); }
/** Test of unProxyObject method, of class AuditAccessor. */ @Test public void testUnProxyObject() throws Exception { System.out.println("unProxyObject"); Trade trade = (Trade) HibernateUtil.getObjectWithQuery( "from Trade t where t.tradeId in (select min(tradeId) from Trade)"); AuditAccessor.unProxyObject(trade); assertNotNull(" failed unproxying trade", trade); }
/** Test of getPreviousVersion method, of class AuditAccessor. */ @Test public void testGetPreviousVersion() { System.out.println("getPreviousVersion"); Trade entity = (Trade) HibernateUtil.getObjectWithQuery( "from Trade t where t.tradeId in (select min(tradeId) from Trade)"); Trade result = AuditAccessor.getPreviousVersion(entity); assertNotNull(" trade version not found", result); }
/** Test of getOldVersionList method, of class AuditAccessor. */ @Test public void testGetOldVersionList() { System.out.println("getOldVersionList"); Trade trade = (Trade) HibernateUtil.getObjectWithQuery( "from Trade t where t.tradeId in (select min(tradeId) from Trade)"); List<Trade> result = AuditAccessor.getOldVersionList(trade); assertNotNull(" trade versions not found", result); assertFalse(" trade versions not found", result.isEmpty()); }
public static Trade getPreviousVersion(Trade entity) { Session session = HibernateUtil.getSession(); Trade trade = null; try { List<Trade> oldVersionList = getOldVersionList(entity); if (!oldVersionList.isEmpty()) { trade = oldVersionList.get(0); } if (trade != null) { unProxyObject(trade); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | HibernateException | ClassNotFoundException e) { logger.error(StringUtils.formatErrorMessage(e)); } finally { session.close(); } return trade; }
/** * @param proxy * @return * @throws ClassNotFoundException */ public static Object loadLazyObject(HibernateProxy proxy) throws ClassNotFoundException { LazyInitializer init = proxy.getHibernateLazyInitializer(); logger.debug(init.getEntityName() + StringUtils.SPACE + init.getIdentifier()); return HibernateUtil.getObject(Class.forName(init.getEntityName()), init.getIdentifier()); }