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:de.voolk.marbles.persistence.services.PageService.java

@Override
public boolean hasChildren(IPage page) {
    Query query = getEntityManager().createNamedQuery("page:hasChildren");
    query.setParameter("parent", page);
    return ((Number) query.getSingleResult()).intValue() > 0;
}

From source file:de.voolk.marbles.persistence.services.AuthentificationService.java

@Override
public User findUserByName(String name) {
    Query query = entityManager.createNamedQuery("user:byName");
    query.setParameter("name", name);
    try {//from  www  .j  a v  a 2  s .  c  om
        return (User) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:de.voolk.marbles.persistence.services.AuthentificationService.java

@Override
public Role findRoleByName(String name) {
    Query query = entityManager.createNamedQuery("role:byName");
    query.setParameter("name", name);
    try {/*ww w .  j  a  v  a 2  s .  c o m*/
        return (Role) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.opens.tanaguru.entity.dao.tgol.contract.TanaguruSurveyContractDAOImpl.java

/**
 * Native sql query :/*from  ww  w  .j a  v  a  2  s  . c om*/
 * SELECT count(Id_Contract) FROM TGSI_CONTRACT as tc
 *       INNER JOIN TGSI_USER as tu on (tu.Id_User=tc.USER_Id_User)
 *       WHERE tu.Email1 like :prefix;
 * 
 * @param prefix
 * @return
 *      the number of contract for user whom Email1 field starts with prefix
 */
@Override
public int findNumberOfContractsFromPrefix(String prefix) {
    StringBuilder queryString = new StringBuilder();
    queryString.append(FIND_NUMBER_OF_CONTRACT_QUERY);
    Query query = entityManager.createNativeQuery(queryString.toString());
    query.setParameter("prefix", prefix + "%");
    Object result;
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
        return Integer.valueOf(0);
    }
    if (result instanceof BigInteger) {
        return ((BigInteger) result).intValue();
    } else if (result instanceof Integer) {
        return ((Integer) result);
    } else {
        return Integer.valueOf(0);
    }
}

From source file:com.mothsoft.alexis.dao.ModelDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*from   ww w  .  j  a  v  a2s  .  c o  m*/
public DataRange<Model> listByOwner(Long userId, int first, int count) {
    final Query query = this.em
            .createQuery("SELECT m FROM Model m JOIN m.topic t WHERE t.userId = :userId ORDER BY m.name ASC");
    query.setParameter("userId", userId);
    query.setFirstResult(first);
    query.setMaxResults(count);

    final Query query2 = this.em
            .createQuery("SELECT COUNT(m.id) FROM Model m JOIN m.topic t WHERE t.userId = :userId");
    query2.setParameter("userId", userId);
    int total = ((Number) query2.getSingleResult()).intValue();

    final List<Model> list = query.getResultList();
    final DataRange<Model> range = new DataRange<Model>(list, first, total);
    return range;
}

From source file:com.cimpoint.mes.server.repositories.LotRepository.java

public String getNextLotNumber() throws Exception {
    Query qry = getEntityManager().createQuery("select o from ELot o order by o.number desc").setMaxResults(1);
    try {//from  ww  w .j  a v a 2s  .  c  o m
        ELot lot = (ELot) qry.getSingleResult();
        return String.valueOf(Integer.parseInt(lot.getNumber()) + 1);
    } catch (NoResultException ex) {
        return this.getInitLotNumber();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}

From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public Board boardGet(long seq) {
    Query query = this.em
            .createQuery("SELECT board FROM Board board WHERE board.useYn ='Y' and board.nttId =:nttId");
    query.setParameter("nttId", seq);
    return (Board) query.getSingleResult();
}

From source file:de.voolk.marbles.persistence.services.AuthentificationService.java

@Override
public User findUserByIdentKey(String identKey) {
    Query query = entityManager.createNamedQuery("user:byIdentKey");
    query.setParameter("identKey", identKey);
    try {//  www .j  a va2s  . c  o m
        return (User) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:nc.noumea.mairie.appock.dao.impl.SousFamilleDaoImpl.java

@Override
public SousFamille findByLibelleAndCatalogue(String libelle, Catalogue catalogue) {
    // @formatter:off
    String jpaQuery = "   SELECT sousFamille FROM SousFamille sousFamille "
            + "         left join sousFamille.famille famille "
            + "         left join famille.catalogue catalogue " + "       WHERE sousFamille.libelle = :libelle "
            + "         AND catalogue = :catalogue";
    // @formatter:on

    Query query = persistentManager.getEntityManager().createQuery(jpaQuery);

    AppockUtil.setParameter(query, "libelle", libelle);
    AppockUtil.setParameter(query, "catalogue", catalogue);

    try {/*from  w  w w.j  a v  a2s.c o m*/
        return (SousFamille) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.syncope.core.persistence.dao.impl.TaskDAOImpl.java

@Override
public <T extends Task> int count(final Class<T> reference) {
    Query countQuery = entityManager.createNativeQuery("SELECT COUNT(id) FROM Task WHERE DTYPE=?1");
    countQuery.setParameter(1, reference.getSimpleName());

    return ((Number) countQuery.getSingleResult()).intValue();
}