QueryManager implements IQueryManager { @Override public Collection<IMessageUnitEntity> getMessageUnitsWithId(String messageId, Direction... direction) throws PersistenceException { List<MessageUnit> jpaResult = null; final EntityManager em = EntityManagerUtil.getEntityManager(); StringBuilder queryString = new StringBuilder(); queryString.append("SELECT mu ") .append("FROM MessageUnit mu ") .append("WHERE mu.MESSAGE_ID = :msgId "); if (direction.length == 1) queryString.append("AND mu.DIRECTION = :direction "); queryString.append("ORDER BY mu.MU_TIMESTAMP"); try { em.getTransaction().begin(); final TypedQuery<MessageUnit> query = em.createQuery(queryString.toString(), MessageUnit.class) .setParameter("msgId", messageId); if (direction.length == 1) query.setParameter("direction", direction[0]); jpaResult = query.getResultList(); } catch (final Exception e) { throw new PersistenceException("Could not execute query \"getMessageUnitsWithId\"", 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 getMessageUnitsWithId() throws PersistenceException { Collection<IMessageUnitEntity> result = queryManager.getMessageUnitsWithId(TestData.userMsg1.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(1 , result.size()); result = queryManager.getMessageUnitsWithId("non-existing-msgid"); assertTrue(Utils.isNullOrEmpty(result)); result = queryManager.getMessageUnitsWithId(TestData.receipt2.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(3 , result.size()); result = queryManager.getMessageUnitsWithId(TestData.receipt2.getMessageId(), Direction.OUT); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(1 , result.size()); result = queryManager.getMessageUnitsWithId(TestData.error4.getMessageId()); assertFalse(Utils.isNullOrEmpty(result)); assertEquals(2 , result.size()); }