@Override
 public Deployment getLastDeployment(Application app) {
   Query q =
       entityManager.createQuery(
           "select distinct d "
               + "from Deployment d join d.application "
               + "where d.application.id=:id "
               + "order by d.createDate desc");
   q.setParameter("id", app.getId());
   q.setMaxResults(1);
   try {
     Object o = q.getSingleResult();
     return (Deployment) o;
   } catch (NoResultException nre) {
     return null;
   }
 }
 @Override
 public List<Deployment> findDeploymentsByApplication(Application app) {
   Query q =
       entityManager.createQuery(
           "select d "
               + "from Deployment d "
               + "inner join fetch d.applicationArchive aa "
               + "inner join d.application a "
               + "where a.name=:name");
   q.setParameter("name", app.getName());
   try {
     @SuppressWarnings(value = {"unchecked"})
     List<Deployment> deployments = (List<Deployment>) q.getResultList();
     return deployments;
   } catch (NoResultException nre) {
     return null;
   }
 }
 @Override
 public ApplicationArchive findApplicationArchiveByHashAndAlgorithm(
     Application app, String hash, String hashAlgorithm) {
   Query q =
       entityManager.createQuery(
           "select distinct ar "
               + "from ApplicationArchive ar "
               + "join fetch ar.application app "
               + "where ar.hash=:hash "
               + "and ar.hashAlgorithm=:hashAlgorithm "
               + "and app.id=:appId");
   q.setParameter("hash", hash);
   q.setParameter("hashAlgorithm", hashAlgorithm);
   q.setParameter("appId", app.getId());
   q.setMaxResults(1);
   try {
     ApplicationArchive o = (ApplicationArchive) q.getSingleResult();
     return (ApplicationArchive) o;
   } catch (NoResultException nre) {
     return null;
   }
 }