QueryManager implements IQueryManager { @Override public int getNumberOfTransmissions(IUserMessageEntity userMessage) throws PersistenceException { int result = 0; final EntityManager em = EntityManagerUtil.getEntityManager(); final String query = "SELECT COUNT(s1.STATE) " + "FROM UserMessage um JOIN um.states s1 " + "WHERE um.MESSAGE_ID = :msgId " + "AND s1.STATE = :state"; try { em.getTransaction().begin(); result = em.createQuery(query, Long.class) .setParameter("msgId", userMessage.getMessageId()) .setParameter("state", ProcessingState.SENDING) .getSingleResult().intValue(); } catch (final NoResultException nothingFound) { result = 0; } catch (final Exception e) { throw new PersistenceException("Could not execute query \"getNumberOfTransmissions\"", e); } finally { em.getTransaction().commit(); em.close(); } return result; }  @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 getNumberOfTransmissions() throws PersistenceException { assertEquals(1, queryManager.getNumberOfTransmissions(new UserMessageEntity(new UserMessage(TestData.userMsg6)))); assertEquals(2, queryManager.getNumberOfTransmissions(new UserMessageEntity(new UserMessage(TestData.userMsg2)))); assertEquals(0, queryManager.getNumberOfTransmissions(new UserMessageEntity(new UserMessage(TestData.userMsg5)))); }