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

public ContextRule getRuleById(String id) throws SocialSiteException {
    if (id == null) {
        throw new SocialSiteException("userid is null");
    }//  w  ww  .jav  a  2 s  .co  m
    Query query = strategy.getNamedQuery("ContextRule.getById");
    query.setParameter(1, id);
    try {
        return (ContextRule) query.getSingleResult();
    } catch (NonUniqueResultException ne) {
        throw new SocialSiteException(String.format("ERROR: more than one rule with id: %s", id));
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:org.fabric3.samples.bigbank.loan.store.persistent.JPAStoreService.java

public LoanRecord findBySSN(String ssn) throws StoreException {
    try {/*ww w.  ja  v  a 2s . c o  m*/
        Query query = em.createQuery("SELECT r FROM LoanRecord r WHERE r.ssn = :ssn");
        query.setParameter("ssn", ssn);
        return (LoanRecord) query.getSingleResult();
    } catch (PersistenceException e) {
        throw new StoreException(e);
    }
}

From source file:org.kuali.mobility.user.dao.UserDaoImpl.java

public User findUserByGuid(Long guid) {
    Query query = entityManager.createQuery("select u from User u where u.guid = :guid");
    query.setParameter("guid", guid);
    try {//from   w w w  .  j  a  va  2  s .c om
        return (User) query.getSingleResult();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.june.app.user.repository.jpa.UserRepositoryImpl.java

@Override
public UserInfo getUser(String userId) {

    Query query = this.em.createQuery("SELECT userInfo FROM UserInfo userInfo WHERE userInfo.userId =:userId");
    query.setParameter("userId", userId);

    return (UserInfo) query.getSingleResult();
}

From source file:Professor.java

  public Professor findProfessor(int empId) {
  Query q = em.createQuery("SELECT e FROM Professor e WHERE e.id =  ?1");
  q.setParameter(1, empId);/*from   w ww  .  ja va 2s. c  o m*/
  try {
    return (Professor) q.getSingleResult();
  } catch (NoResultException e) {
    return null;
  }
}

From source file:com.iselect.kernal.geo.dao.CountryDaoImpl.java

@Override
public CountryModel getCountryById(Long id) throws EntityNotFoundException {
    CountryModel model = null;//from   ww  w  .  j a v  a 2 s.com
    Query query = this.em.createNamedQuery("country.findById", CountryModelImpl.class);
    query.setParameter("id", id);
    try {
        model = (CountryModel) query.getSingleResult();
    } catch (NoResultException nre) {
        throw new EntityNotFoundException(id.toString(), nre);
    }
    return model;
}

From source file:org.kuali.mobility.configparams.dao.ConfigParamDaoImpl.java

public ConfigParam findConfigParamById(Long id) {
    Query query = entityManager.createQuery("select cp from ConfigParam cp where cp.configParamId = :id");
    query.setParameter("id", id);
    return (ConfigParam) query.getSingleResult();
}

From source file:org.kuali.mobility.configparams.dao.ConfigParamDaoImpl.java

public ConfigParam findConfigParamByName(String name) {
    Query query = entityManager.createQuery("select cp from ConfigParam cp where cp.name like :name");
    query.setParameter("name", name);
    return (ConfigParam) query.getSingleResult();
}

From source file:com.formkiq.core.dao.AbstractDaoImpl.java

/**
* Return a single result, catch NoResultException and return NULL.
* @param query Query//from   w  w  w. j a  v a 2 s  .c  o m
* @return Object
*/
protected Object getSingleResult(final Query query) {

    Object t;

    try {

        t = query.getSingleResult();

    } catch (NoResultException e) {
        t = null;
    }

    return t;
}

From source file:org.fabric3.samples.bigbank.loan.store.persistent.JPAStoreService.java

public LoanRecord find(long id) throws StoreException {
    try {//from  www.  j  a  v a  2 s .  c om
        Query query = em.createQuery("SELECT r FROM LoanRecord r WHERE r.id = :number");
        query.setParameter("number", id);
        return (LoanRecord) query.getSingleResult();
    } catch (PersistenceException e) {
        throw new StoreException(e);
    }
}