Example usage for javax.persistence Query getSingleResult

List of usage examples for javax.persistence Query getSingleResult

Introduction

In this page you can find the example usage for javax.persistence Query getSingleResult.

Prototype

Object getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single untyped result.

Usage

From source file:com.openmeap.model.ModelServiceImpl.java

@Override
public ApplicationArchive getApplicationArchiveByDeployment(Deployment depl) {
    Query q = entityManager.createQuery("select distinct aa " + "from ApplicationArchive aa, Deployment d  "
            + "where d.applicationArchive=aa and d.id=:id ");
    q.setParameter("id", depl.getId());
    q.setMaxResults(1);//from  w ww  .j ava  2 s  . c  o  m
    try {
        Object o = q.getSingleResult();
        return ((ApplicationArchive) o);
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:net.kamhon.ieagle.dao.Jpa2Dao.java

@SuppressWarnings("unchecked")
public T findUnique(String query, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    Query q = em.createQuery(query2);
    for (int i = 0; i < objectArray.length; i++) {
        q.setParameter(i + 1, objectArray[i]);
    }/*  ww  w  . j a  v a2s  .c o  m*/
    return (T) q.getSingleResult();
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.jpa.ArticleHandler.java

/**
 * Deletes a news article from the db/*from w ww  . j  a v  a 2 s.  c  om*/
 *
 * @param entity The article to delete
 */
public void delete(UXBEntity entity) throws Exception {

    EntityManager em = null;
    EntityTransaction tx = null;
    try {

        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();

        //         Query query = em.createQuery(new StringBuilder().append("SELECT x FROM Article x WHERE x.aid = ").append(entity.getUuid()).append(" AND x.language='").append(entity.getLanguage()).append("'").toString());
        Query query = em.createQuery(new StringBuilder()
                .append("SELECT x FROM article x WHERE x.aid = :fsid AND x.language=:language").toString());
        query.setParameter("fsid", Long.parseLong(entity.getUuid()));
        query.setParameter("language", entity.getLanguage());

        if (!query.getResultList().isEmpty()) {
            Article art = (Article) query.getSingleResult();
            em.remove(art);
        }
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.setRollbackOnly();
            throw e;
        }
        logger.error("Failure while deleting from the database", e);
    } finally {
        if (tx != null && tx.isActive()) {
            if (tx.getRollbackOnly()) {
                tx.rollback();
            }
        }
        if (em != null) {
            em.close();
        }
    }
}

From source file:com.telefonica.euro_iaas.paasmanager.dao.impl.ApplicationInstanceDaoJpaImpl.java

/**
 * Get an application instance given its name and vdc.
 * @param name/*from   www.j  a  va2  s.  c o m*/
 * @param vdc
 * @return
 * @throws EntityNotFoundException
 */
public ApplicationInstance load(String name, String vdc) throws EntityNotFoundException {
    Query query = getEntityManager()
            .createQuery("select p from ApplicationInstance p where p.name = :name and p.vdc = :vdc");
    query.setParameter("name", name);

    query.setParameter("vdc", vdc);
    if (vdc == null) {

        query.setParameter("vdc", "");
    } else {
        query.setParameter("vdc", vdc);
    }
    ApplicationInstance app = null;
    try {
        app = (ApplicationInstance) query.getSingleResult();
    } catch (NoResultException e) {
        String message = " No app found in the database with id: " + name + " and vdc " + vdc + " Exception: "
                + e.getMessage();

        throw new EntityNotFoundException(Network.class, "name", name);
    }
    return app;
}

From source file:com.sun.socialsite.business.impl.JPARelationshipManagerImpl.java

public Relationship getRelationship(Profile from, Profile to) throws SocialSiteException {
    if (from == null)
        throw new SocialSiteException("from is null");
    if (to == null)
        throw new SocialSiteException("to is null");

    ProfileManager pmgr = Factory.getSocialSite().getProfileManager();
    Query query = strategy.getNamedQuery("Relationship.getByProfileFromAndProfileTo");
    query.setParameter(1, from);/*from w  ww.j  a  va  2s. c om*/
    query.setParameter(2, to);
    try {
        return (Relationship) query.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:com.openmeap.model.ModelServiceImpl.java

@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);//from  w ww. ja  va 2  s.  com
    try {
        Object o = q.getSingleResult();
        return (Deployment) o;
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:com.sun.socialsite.business.impl.JPARelationshipManagerImpl.java

public void removeRelationships(Profile profile1, Profile profile2) throws SocialSiteException {

    Query query1 = strategy.getNamedQuery("Relationship.getByProfileFromAndProfileTo");
    query1.setParameter(1, profile1);//from  w  ww .  j a  va2s .  c o  m
    query1.setParameter(2, profile2);
    Relationship rel1 = (Relationship) query1.getSingleResult();
    strategy.remove(rel1);

    Query query2 = strategy.getNamedQuery("Relationship.getByProfileFromAndProfileTo");
    query2.setParameter(1, profile2);
    query2.setParameter(2, profile1);
    Relationship rel2 = (Relationship) query2.getSingleResult();
    strategy.remove(rel2);
}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@Override
public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Long id2, Long id3, Class<T> clazz) {
    try {/* w w  w .j a  v  a  2s. c  o  m*/
        String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class);
        Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1).setParameter("o2Id", id2)
                .setParameter("o3Id", id3);
        return (Long) query.getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findIdByXPath(Long id1, Long id2, Long id3) Exception: %s - %s\n",
                clazz.toString(), e.toString());
        throw new RuntimeException(e);
    }
}

From source file:net.kamhon.ieagle.dao.JpaDao.java

@Override
public T findUnique(String query, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    return getJpaTemplate().execute(new JpaCallback<T>() {
        @SuppressWarnings("unchecked")
        @Override//from   ww w  .  j av a2s  .c  o  m
        public T doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery(query2);
            for (int i = 0; i < objectArray.length; i++) {
                q.setParameter(i + 1, objectArray[i]);
            }
            return (T) q.getSingleResult();
        }
    });
}

From source file:com.sun.socialsite.business.impl.JPARelationshipManagerImpl.java

public RelationshipRequest getRelationshipRequest(Profile from, Profile to) throws SocialSiteException {
    if (from == null)
        throw new SocialSiteException("profileFrom is null");
    if (from == null)
        throw new SocialSiteException("profileTo is null");
    Query query = strategy.getNamedQuery("RelationshipRequest.getByProfileFromAndProfileTo");
    query.setParameter(1, from);/* w w w. j ava  2  s. c  o  m*/
    query.setParameter(2, to);
    try {
        return (RelationshipRequest) query.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}