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.JPAProfileManagerImpl.java

public Profile getProfileByUserId(String userid) throws SocialSiteException {
    if (userid == null)
        throw new SocialSiteException("userid is null");
    Query query = strategy.getNamedQuery("Profile.getByUserId");
    query.setParameter(1, userid);/*w  w w  . j  a  va2s . c om*/
    try {
        return (Profile) query.getSingleResult();
    } catch (NonUniqueResultException ne) {
        throw new SocialSiteException("ERROR: more than one user with userid: " + userid, ne);
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:es.ucm.fdi.dalgs.group.repository.GroupRepository.java

public Group existInCourse(Long id_course, String name) {// , Long id) {
    Course course = em.getReference(Course.class, id_course);

    Query query = null;

    query = em.createQuery("select g from Group g  where g.course = ?1 and g.name = ?2");
    query.setParameter(1, course);//  w w  w .  java  2s .co  m
    query.setParameter(2, name);

    if (query.getResultList().isEmpty())
        return null;
    else
        return (Group) query.getSingleResult();
}

From source file:es.ucm.fdi.dalgs.topic.repository.TopicRepository.java

public Topic existByCode(String code, Long id_module) {
    Module module = em.getReference(Module.class, id_module);
    Query query = em.createQuery("select t from Topic t where t.info.code=?1 and t.module = ?2");
    query.setParameter(1, code);//  w ww  . j  a  v  a 2 s .c om
    query.setParameter(2, module);
    if (query.getResultList().isEmpty())
        return null;
    else
        return (Topic) query.getSingleResult();
}

From source file:com.exp.tracker.services.impl.JpaExpenseService.java

@Transactional(readOnly = true)
public ExpenseDetail getExpenseById(Long id) {
    ExpenseEntity returnExpenseEntity = null;
    Query queryGetExpenseById = em.createNamedQuery("getExpenseById");
    queryGetExpenseById.setParameter("id", id);
    ExpenseEntity ee = (ExpenseEntity) queryGetExpenseById.getSingleResult();
    if (null != ee.getSettlementId()) {
        // this means record has been settled.
        Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
        queryGetSettlementForId.setParameter("id", ee.getSettlementId());
        SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
        for (ExpenseEntity ee1 : se.getExpenseSet()) {
            if (id.longValue() == ee1.getId().longValue()) {
                returnExpenseEntity = ee1;
                break;
            }//  ww w . j av  a  2  s  .  co  m
        }
    } else {
        returnExpenseEntity = ee;
    }
    return new ExpenseDetail(returnExpenseEntity);
}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

public AcademicTerm exists(String term, Degree degree) {
    Query query = null;
    query = em.createQuery("select a from AcademicTerm a where  a.term=?1 and a.degree = ?2");
    query.setParameter(1, term);/* w w  w  .  j ava2  s .c  o m*/
    query.setParameter(2, degree);

    if (query.getResultList().isEmpty())
        return null;
    else
        return (AcademicTerm) query.getSingleResult();

}

From source file:es.ucm.fdi.dalgs.competence.repository.CompetenceRepository.java

public Competence existByCode(String code, Degree degree) {
    Query query = em.createQuery("Select c from Competence c where c.info.code=?1 and c.degree=?2");
    query.setParameter(1, code);/*  w  w  w.j a  v a 2s.  c  o  m*/
    query.setParameter(2, degree);

    if (query.getResultList().isEmpty())
        return null;
    else
        return (Competence) query.getSingleResult();
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public int itemsDateRangeCount(Date start, Date end) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.select(cb.count(rt.get(ActivityAIPC_.actinstproccodeser)));
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();

}

From source file:mx.edu.ittepic.proyectofinal.ejbs.ejbUsers.java

public String getUser(String username, String password) {
    Message m = new Message();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();/*from   ww w  .  ja  v a  2 s  .c  o  m*/
    String md5 = DigestUtils.md5Hex(password);
    try {
        Users users;
        Query q = entity.createNamedQuery("Users.getUser").setParameter("username", username)
                .setParameter("password", md5);

        users = (Users) q.getSingleResult();

        Role role = users.getRoleid();

        m.setCode(200);
        m.setMsg("Tiene acceso al sistema");
        m.setDetail(users.getUsername() + ":" + users.getApikey() + ":" + role.getRoleid());

    } catch (NoResultException e) {
        m.setCode(401);
        m.setMsg("No tiene acceso al sistema");
        m.setDetail(e.toString());
    }
    return gson.toJson(m);
}

From source file:es.ucm.fdi.dalgs.activity.repository.ActivityRepository.java

public Activity existByCode(String code) {
    Query query = em.createQuery("Select a from Activity a where a.info.code=?1");
    query.setParameter(1, code);/*w  w w . j  a  va  2  s  . co  m*/

    if (query.getResultList().isEmpty())
        return null;
    else
        return (Activity) query.getSingleResult();
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.ApplicationDAOJPAImpl.java

@Override
public void updateApplication(String realm, Application application) {
    Query query = null;
    query = em.createQuery("select a from Application a where a.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    ApplicationEntity applicationEntity = (ApplicationEntity) query.getSingleResult();

    domain2entity(application, applicationEntity);

    em.persist(applicationEntity);/*  w  w  w. j  a  v  a 2  s. c  om*/

    LOG.debug("Application '{}' updated", realm);
}