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:org.jnap.core.persistence.jpa.DaoSupport.java

/**
 * //www  .  j  a v a 2  s.co m
 * @param query
 * @return
 */
protected Long count(Query query) {
    Number quantity = (Number) query.getSingleResult();
    return quantity == null ? 0 : quantity.longValue();
}

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

@Override
public void addClaimToApplication(Application application, RequestClaim claim) {
    ApplicationEntity applicationEntity = null;
    if (application.getId() != 0) {
        applicationEntity = em.find(ApplicationEntity.class, application.getId());
    } else {/*from w w  w. jav  a 2s  .c  om*/
        Query query = null;
        query = em.createQuery("select a from Application a where a.realm=:realm");
        query.setParameter("realm", application.getRealm());

        applicationEntity = (ApplicationEntity) query.getSingleResult();
    }

    Claim c = claimDAO.getClaim(claim.getClaimType().toString());
    ClaimEntity claimEntity = em.find(ClaimEntity.class, c.getId());

    ApplicationClaimEntity appClaimEntity = new ApplicationClaimEntity();
    appClaimEntity.setClaim(claimEntity);
    appClaimEntity.setApplication(applicationEntity);
    appClaimEntity.setOptional(claim.isOptional());

    applicationEntity.getRequestedClaims().add(appClaimEntity);
}

From source file:org.opentides.dao.impl.SystemCodesDaoJpaImpl.java

@Override
public long countDuplicate(SystemCodes code) {
    String queryString = getJpqlQuery("jpql.systemcodes.countDuplicate");
    Query queryObject = getEntityManager().createQuery(queryString);
    queryObject.setParameter("keyName", code.getKey());
    // if id is null it means it is a new systemCodes
    if (code.getId() == null) {
        queryObject.setParameter("id", 0l);
    } else {/*from   www. j a  va 2s .co  m*/
        queryObject.setParameter("id", code.getId());
    }
    return (Long) queryObject.getSingleResult();
}

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

@Override
public void removeClaimFromApplication(Application application, RequestClaim claim) {
    ApplicationEntity applicationEntity = null;
    if (application.getId() != 0) {
        applicationEntity = em.find(ApplicationEntity.class, application.getId());
    } else {//from   w ww. j  ava 2  s.  c o  m
        Query query = null;
        query = em.createQuery("select a from Application a where a.realm=:realm");
        query.setParameter("realm", application.getRealm());

        applicationEntity = (ApplicationEntity) query.getSingleResult();
    }

    ApplicationClaimEntity foundEntity = null;
    for (ApplicationClaimEntity acm : applicationEntity.getRequestedClaims()) {
        if (claim.getClaimType().toString().equals(acm.getClaim().getClaimType())) {
            foundEntity = acm;
            break;
        }
    }
    if (foundEntity == null) {
        throw new EntityNotFoundException("ApplicationClaimEntity not found");
    }

    applicationEntity.getRequestedClaims().remove(foundEntity);
}

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

@Transactional(readOnly = true)
private Map<String, Object> getSettlementReportParamMap(Long sid) {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
    queryGetSettlementForId.setParameter("id", sid);
    SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
    paramMap.put("reportTitle", "Settlement Report");
    paramMap.put("startDate", se.getStartDate());
    paramMap.put("endDate", se.getEndDate());
    paramMap.put("volume", se.getVolume());
    return paramMap;
}

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

@Transactional(readOnly = true)
private Map<String, Object> getExpenseReportParamMap(Long sid) {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
    queryGetSettlementForId.setParameter("id", sid);
    SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
    paramMap.put("reportTitle", "Expense Report");
    paramMap.put("startDate", se.getStartDate());
    paramMap.put("endDate", se.getEndDate());
    paramMap.put("totalVolume", se.getVolume());
    return paramMap;
}

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

private JRDataSource getSettlementDataSource(Long sid) {
    // get data from back end
    Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
    queryGetSettlementForId.setParameter("id", sid);
    SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
    // wrap it up
    SettlementBean sb = new SettlementBean(se);
    // create data source
    SettlementGenerationDataSource sgds = new SettlementGenerationDataSource(sb);
    // return data
    return sgds;//from   w  w w.j a  v  a2  s . c  o  m
}

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

public String GetUserByID(String userid) {
    Message m = new Message();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();/*from  w w  w  . ja  v  a  2 s  .c o m*/

    try {
        Users users;
        Query q = entity.createNamedQuery("Users.findByUserid").setParameter("userid",
                Integer.parseInt(userid));

        users = (Users) q.getSingleResult();

        users.setSaleList(null);

        m.setCode(200);
        m.setMsg(gson.toJson(users));
        m.setDetail("OK");
    } catch (NoResultException e) {
        m.setCode(404);
        m.setMsg("No se encontro el registro");
        m.setDetail(e.toString());
    }
    return gson.toJson(m);
}

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

public String getCompanyidByUserID(String userid) {
    Message m = new Message();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();/*from w  ww.j  a va2s . c o  m*/

    try {
        Users users;
        Query q = entity.createNamedQuery("Users.findCompanyid").setParameter("userid",
                Integer.parseInt(userid));

        users = (Users) q.getSingleResult();

        m.setCode(200);
        m.setMsg(gson.toJson(users));
        m.setDetail("OK");
    } catch (Exception e) {
        m.setCode(404);
        m.setMsg("No se encontro el registro");
        m.setDetail(e.toString());
    }
    return gson.toJson(m);

}

From source file:cn.guoyukun.spring.jpa.repository.RepositoryHelper.java

/**
 * <p>?<br/>/*from   w w w  . j  a  v  a 2s  .co  m*/
 * ?{@see cn.guoyukun.spring.jpa.repository.UserRepositoryImplForCustomSearchIT}
 * {@see cn.guoyukun.spring.jpa.repository.UserRepositoryImplForDefaultSearchIT}
 *
 * @param ql
 * @param searchable
 * @param searchCallback
 * @return
 */
public long count(final String ql, final Searchable searchable, final SearchCallback searchCallback) {

    assertConverted(searchable);

    StringBuilder s = new StringBuilder(ql);
    searchCallback.prepareQL(s, searchable);
    Query query = getEntityManager().createQuery(s.toString());
    applyEnableQueryCache(query);
    searchCallback.setValues(query, searchable);

    return (Long) query.getSingleResult();
}