QueryManager implements IQueryManager { @Override public <V extends IMessageUnitEntity> void ensureCompletelyLoaded(V messageUnit) throws PersistenceException { if (messageUnit.isLoadedCompletely()) return; MessageUnitEntity providerEntityObject = (MessageUnitEntity) messageUnit; EntityManager em = EntityManagerUtil.getEntityManager(); try { em.getTransaction().begin(); final MessageUnit actual = em.find(MessageUnit.class, providerEntityObject.getOID()); loadCompletely(actual); providerEntityObject.updateJPAObject(actual); providerEntityObject.setMetadataLoaded(true); } catch (final Exception e) { throw new PersistenceException("Could not load the object from the database", e); } finally { em.getTransaction().commit(); em.close(); } }  @Override List<V> getMessageUnitsInState(
               Class<T> type, Direction direction, ProcessingState[] states); @Override Collection<IMessageUnitEntity> getMessageUnitsWithId(String messageId, 
    															Direction... direction); @Override Collection<IMessageUnitEntity> getMessageUnitsWithLastStateChangedBefore(Date maxLastChangeDate); @Override List<V> getMessageUnitsForPModesInState(Class<T> type,
                                    Collection<String> pmodeIds, ProcessingState state); @Override void ensureCompletelyLoaded(V messageUnit); static void loadCompletely(MessageUnit jpaMessageUnit); @Override int getNumberOfTransmissions(IUserMessageEntity userMessage); @Override boolean isAlreadyProcessed(final IUserMessage userMessage); List<IMessageUnitEntity> getMessageUnitHistory(final Date start, final int max);  }
@Test public void ensureCompletelyLoaded() throws PersistenceException { Collection<IMessageUnitEntity> result = queryManager.getMessageUnitsWithId(TestData.userMsg1.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); UserMessageEntity userMessage = (UserMessageEntity) result.iterator().next(); assertFalse(userMessage.isLoadedCompletely()); queryManager.ensureCompletelyLoaded(userMessage); assertTrue(userMessage.isLoadedCompletely()); assertNotNull(userMessage.getSender()); assertTrue(CompareUtils.areEqual(TestData.userMsg1.getSender(), userMessage.getSender())); assertNotNull(userMessage.getReceiver()); assertTrue(CompareUtils.areEqual(TestData.userMsg1.getReceiver(), userMessage.getReceiver())); assertFalse(Utils.isNullOrEmpty(userMessage.getPayloads())); IPayload retrievedPayload = userMessage.getPayloads().iterator().next(); IPayload originalPayload = TestData.userMsg1.getPayloads().iterator().next(); assertEquals(originalPayload.getContainment(), retrievedPayload.getContainment()); assertFalse(Utils.isNullOrEmpty(retrievedPayload.getProperties())); assertTrue(CompareUtils.areEqual(retrievedPayload.getProperties().iterator().next(), originalPayload.getProperties().iterator().next())); assertEquals(originalPayload.getDescription().getText(), retrievedPayload.getDescription().getText()); assertEquals(originalPayload.getDescription().getLanguage(), retrievedPayload.getDescription().getLanguage()); assertFalse(Utils.isNullOrEmpty(userMessage.getMessageProperties())); assertTrue(CompareUtils.areEqual(TestData.userMsg1.getMessageProperties().iterator().next(), userMessage.getMessageProperties().iterator().next())); result = queryManager.getMessageUnitsWithId(TestData.receipt1.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); ReceiptEntity receipt = (ReceiptEntity) result.iterator().next(); assertTrue(receipt.isLoadedCompletely()); assertNull(receipt.getContent()); result = queryManager.getMessageUnitsWithId(TestData.error3.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); ErrorMessageEntity error = (ErrorMessageEntity) result.iterator().next(); assertFalse(error.isLoadedCompletely()); queryManager.ensureCompletelyLoaded(error); assertTrue(error.isLoadedCompletely()); assertFalse(Utils.isNullOrEmpty(error.getErrors())); IEbmsError originalError = TestData.error3.getErrors().iterator().next(); IEbmsError retrievedError = error.getErrors().iterator().next(); assertEquals(originalError.getErrorCode(), retrievedError.getErrorCode()); assertEquals(originalError.getErrorDetail(), retrievedError.getErrorDetail()); }