QueryManager implements IQueryManager { @Override public <T extends IMessageUnit, V extends IMessageUnitEntity> List<V> getMessageUnitsForPModesInState(Class<T> type, Collection<String> pmodeIds, ProcessingState state) throws PersistenceException { List<T> jpaResult = null; final EntityManager em = EntityManagerUtil.getEntityManager(); Class jpaEntityClass = JPAEntityHelper.determineJPAClass(type); final String queryString = "SELECT mu " + "FROM " + jpaEntityClass.getSimpleName() + " mu JOIN FETCH mu.states s1 " + "WHERE mu.PMODE_ID IN :pmodeIds " + "AND s1.PROC_STATE_NUM = (SELECT MAX(s2.PROC_STATE_NUM) FROM mu.states s2) " + "AND s1.STATE = :state " + "ORDER BY mu.MU_TIMESTAMP"; try { em.getTransaction().begin(); jpaResult = em.createQuery(queryString, jpaEntityClass) .setParameter("pmodeIds", pmodeIds) .setParameter("state", state) .getResultList(); } catch (final Exception e) { throw new PersistenceException("Could not execute query \"getMessageUnitsForPModesInState\"", e); } finally { em.getTransaction().commit(); em.close(); } return JPAEntityHelper.wrapInEntity(jpaResult); }  @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 getMessageUnitsForPModesInState() throws PersistenceException { Set<String> pmodeIds = new HashSet<>(); pmodeIds.add(TestData.userMsg5.getPModeId()); List<IMessageUnitEntity> result = queryManager.getMessageUnitsForPModesInState(IUserMessage.class , pmodeIds , ProcessingState.DELIVERED); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(1 , result.size()); assertTrue(result.get(0) instanceof IUserMessageEntity); result = queryManager.getMessageUnitsForPModesInState(IUserMessage.class, pmodeIds, ProcessingState.READY_TO_PUSH); assertTrue(Utils.isNullOrEmpty(result)); result = queryManager.getMessageUnitsForPModesInState(IMessageUnit.class, pmodeIds, ProcessingState.DELIVERED); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(3 , result.size()); assertSorted(result); pmodeIds.add(TestData.error3.getPModeId()); result = queryManager.getMessageUnitsForPModesInState(IMessageUnit.class, pmodeIds, ProcessingState.CREATED); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(1 , result.size()); assertEquals(TestData.error3.getMessageId(), result.get(0).getMessageId()); pmodeIds.add(TestData.userMsg2.getPModeId()); result = queryManager.getMessageUnitsForPModesInState(IUserMessage.class, pmodeIds, ProcessingState.DELIVERED); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(2 , result.size()); assertEquals(TestData.userMsg2.getMessageId(), result.get(0).getMessageId()); assertEquals(TestData.userMsg5.getMessageId(), result.get(1).getMessageId()); }