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.sun.socialsite.business.impl.JPAGroupManagerImpl.java

public GroupRequest getMembershipRequest(Group group, Profile profile) throws SocialSiteException {
    if (group == null) {
        throw new SocialSiteException("Group is null");
    }//from ww w. j a v  a 2  s . c  o m
    if (profile == null) {
        throw new SocialSiteException("profile is null");
    }
    Query query = strategy.getNamedQuery("GroupRequest.getByGroupAndProfile");
    query.setParameter(1, group);
    query.setParameter(2, profile);
    try {
        return (GroupRequest) query.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

/**
 * /*  ww  w. j a va2 s  .  co  m*/
 * @param id
 * @param isFetchParameters
 * @return
 */
@Override
public Content readWithRelatedContent(Long id, boolean isFetchParameters) {
    Query query = entityManager.createQuery("SELECT c FROM " + getEntityClass().getName() + " c"
            + " WHERE c.id = :id" + " AND c.httpStatusCode =:httpStatusCode");
    query.setParameter("id", id);
    query.setParameter(HTTP_STATUS_CODE_KEY, HTTP_STATUS_OK);
    try {
        Content content = (Content) query.getSingleResult();
        if (content instanceof SSP) {
            ((SSP) content).getRelatedContentSet().size();
            if (isFetchParameters) {
                Audit audit = ((SSP) content).getAudit();
                audit.getParameterSet().size();
            }
        }
        flushAndCloseEntityManager();
        return content;
    } catch (NoResultException nre) {
        return null;
    }
}

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

public GroupRelationship getMembership(Group group, Profile profile) throws SocialSiteException {
    Query query = strategy.getNamedQuery("GroupRelationship.getByGroupAndProfile");
    query.setParameter(1, group);/*from  ww w. j  a  v a 2s  .com*/
    query.setParameter(2, profile);
    try {
        return (GroupRelationship) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:br.org.indt.ndg.server.result.ResultHandlerBean.java

@Override
public ResultXml getResultDB(String idResult) throws MSMApplicationException, MSMSystemException {
    Query q = manager.createNamedQuery("result.findByIdResult");
    q.setParameter("idResult", idResult);
    Result result = null;//  w  w  w . j a va2s .com
    result = (Result) q.getSingleResult();

    ResultParser parser = new ResultParser();
    ResultXml resultXML = null;

    try {
        resultXML = parser.parseResult(new StringBuffer(result.getResultXml()), "UTF-8");
    } catch (SAXException e) {
        throw new ResultNotParsedException();
    } catch (ParserConfigurationException e) {
        throw new ResultNotParsedException();
    } catch (IOException e) {
        throw new ResultNotParsedException();
    }

    return resultXML;
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

@Override
public Long findNumberOfOrphanContentFromWebResource(WebResource webResource) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery(SELECT_DISTINCT_SSP + SSPImpl.class.getName() + " s" + JOIN_WR
                + WEB_RESOURCE_CONDITION + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        return (Long) query.getSingleResult();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery(SELECT_DISTINCT_SSP + SSPImpl.class.getName() + " s" + JOIN_WR
                + " JOIN w.parent p" + " WHERE p=:webResource" + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        return (Long) query.getSingleResult();
    }//from  w  w  w .  j av a2s.co  m
    return (long) 0;
}

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

public boolean removeMembership(Group group, Profile profile) throws SocialSiteException {
    if (isFounder(group, profile)) {
        return false;
    }/*w  ww .ja v  a 2s.c  om*/
    Query query = strategy.getNamedQuery("GroupRelationship.getByGroupAndUserProfile");
    query.setParameter(1, group);
    query.setParameter(2, profile);
    GroupRelationship rel = (GroupRelationship) query.getSingleResult();
    strategy.remove(rel);
    SocialSiteActivityManager amgr = Factory.getSocialSite().getSocialSiteActivityManager();
    amgr.recordActivity(profile, group, SocialSiteActivity.LEFT_GROUP, group.getName());
    return true;
}

From source file:com.sun.socialsite.userapi.UserManagerImpl.java

public User getUserByActivationCode(String activationCode) throws UserManagementException {
    if (activationCode == null) {
        throw new UserManagementException("Cannot get user with null activationCode");
    }/*w w w .j  a  v  a 2s  . c o  m*/
    Query query = getNamedQuery("User.findByActivationCode");
    query.setParameter("activationCode", activationCode);
    User user = null;
    try {
        user = (User) query.getSingleResult();
    } catch (NoResultException ex) {
        user = null;
    }
    return user;
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}// w w w  .jav  a  2  s. c o  m
 */
@Override
public <T> T getScalarResultOfType(Class<T> resultClass, QueryType type, String query, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        switch (type) {
        case JPA:
            TypedQuery<T> typedQuery = em.createQuery(query, resultClass);
            JpaUtil.bindQueryParams(typedQuery, posParams);
            return typedQuery.getSingleResult();
        case NATIVE:
            Query nativeQuery = em.createNativeQuery(query, resultClass);
            JpaUtil.bindQueryParams(nativeQuery, posParams);
            return resultClass.cast(nativeQuery.getSingleResult());
        default:
            throw new IllegalStateException("Invalid QueryType!!");
        }
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

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

public boolean isFounder(Group group, Profile profile) throws SocialSiteException {
    Query query = strategy.getNamedQuery("GroupRelationship.getByGroupAndUserProfile");
    query.setParameter(1, group);//ww  w.j av a2s.  c o m
    query.setParameter(2, profile);
    try {
        GroupRelationship rel = (GroupRelationship) query.getSingleResult();
        if (rel != null) {
            return (rel.getRelcode() == GroupRelationship.Relationship.FOUNDER);
        }
    } catch (NoResultException e) {
        // Do nothing
    }
    return false;
}

From source file:twity.TwityServlet.java

private User fetchUser(String uid) {
    try {/*from ww  w.  j  av a 2  s.  com*/
        EntityManager em = EMF.get().createEntityManager();
        Query jpaQuery = em.createQuery("SELECT u FROM User u WHERE u.userId = :uid");
        jpaQuery.setParameter("uid", uid);
        return (User) jpaQuery.getSingleResult();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}