Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getSingleResult.

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:net.navasoft.madcoin.backend.model.controller.impl.ProvidersDAO.java

/**
 * Gets the by logical id.//  www .  j av a  2  s  . c  om
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:49 PM
 */
@Override
public ServiceProviders getByLogicalId(Serializable idEntity) {
    TypedQuery<ServiceProviders> query = entityManager
            .createNamedQuery("ServiceProviders." + NormalizedEntity.NAMED_QUERY_ID, ServiceProviders.class);
    query.setParameter("appUsername", ((ServiceProvidersPK) idEntity).getAppUsername());
    return query.getSingleResult();
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * /*w  ww  . j a  va 2s .c om*/
 * @return
 */
private Long selectMaxFromOrganisations() {
    try {
        // get all users
        TypedQuery<Long> query = em.createQuery(
                "select max(c.organisation_id) from Organisation c where c.deleted LIKE 'false'", Long.class);
        Long l = query.getSingleResult();
        log.debug("selectMaxFromOrganisations" + l);
        return l;
    } catch (Exception ex2) {
        log.error("[selectMaxFromUsers] ", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

private Long selectMaxUsersByOrganisationId(long organisation_id) {
    try {//from w w w .  j a v a 2s. c o m
        TypedQuery<Long> query = em.createNamedQuery("selectMaxUsersByOrganisationId", Long.class);
        query.setParameter("organisation_id", organisation_id);

        Long l = query.getSingleResult();
        log.debug("selectMaxUsersByOrganisationId" + l);
        return l;

    } catch (Exception ex2) {
        log.error("[getUsersByOrganisationId]", ex2);
    }
    return null;
}

From source file:net.navasoft.madcoin.backend.model.controller.impl.AcceptingProviderDataAccess.java

/**
 * Gets the by logical id.//from w  w w. ja  v  a 2 s  .  c  om
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:45 PM
 */
@Override
public WorkRequestsXServiceProviders getByLogicalId(Serializable idEntity) {
    TypedQuery<WorkRequestsXServiceProviders> query = entityManager.createNamedQuery(
            "WorkRequestsXServiceProviders." + NormalizedEntity.NAMED_QUERY_ID,
            WorkRequestsXServiceProviders.class);
    query.setParameter("idWorkRequest", ((WorkRequestsXServiceProvidersPK) idEntity).getIdWorkRequest());
    query.setParameter("nameServiceProvider",
            ((WorkRequestsXServiceProvidersPK) idEntity).getNameServiceProvider());
    return query.getSingleResult();
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

public Organisation getOrganisationByIdBackup(long organisation_id) {
    try {// w  w  w  .jav  a  2s  .  co m
        TypedQuery<Organisation> query = em.createNamedQuery("getAnyOrganisationById", Organisation.class);
        query.setParameter("organisation_id", organisation_id);
        Organisation o = null;
        try {
            o = query.getSingleResult();
        } catch (NoResultException e) {
            // o = null;
        }
        return o;
    } catch (Exception ex2) {
        log.error("[getOrganisationById]", ex2);
    }
    return null;
}

From source file:com.oncore.calorders.rest.service.extension.OrderHistoryFacadeRESTExtension.java

/**
 * Fetch all orders grouped by status//  w  w  w .  j  a v a2s  .c  om
 *
 * @param departmentId a valid department id
 * @return a structure of order totals grouped by status
 *
 * @throws com.oncore.calorders.core.exceptions.DataAccessException
 */
@GET
@Path("fetchOrderStatusSummary/{departmentId}")
@Produces({ MediaType.APPLICATION_JSON })
public OrderStatusSummaryData fetchOrderStatusSummary(@PathParam("departmentId") Integer departmentId)
        throws DataAccessException {

    OrderStatusSummaryData orderStatusSummaryData = new OrderStatusSummaryData();
    OrderStatusData orderStatusData = null;
    OrdStatusCd ordStatusCd = null;
    Department department = null;

    try {

        Logger.debug(LOG, "Hey testing logging, the fetchOrderStatusSummary is being called!");

        department = getEntityManager().createNamedQuery("Department.findByDepUid", Department.class)
                .setParameter("depUid", departmentId).getSingleResult();

        ordStatusCd = getEntityManager().createNamedQuery("OrdStatusCd.findByCode", OrdStatusCd.class)
                .setParameter("code", "CANC").getSingleResult();

        TypedQuery<Long> query = getEntityManager().createQuery(
                "SELECT COUNT(o) FROM OrderHistory o WHERE o.depUidFk = :departmentId AND o.ordStatusCd = :code",
                Long.class).setParameter("departmentId", department).setParameter("code", ordStatusCd);
        Long count = query.getSingleResult();
        List<Integer> items = new ArrayList<>(1);
        orderStatusData = new OrderStatusData();
        orderStatusData.setName("Cancelled");
        items.add(count.intValue());
        orderStatusData.setItems(items);
        orderStatusSummaryData.getItems().add(orderStatusData);

        ordStatusCd = getEntityManager().createNamedQuery("OrdStatusCd.findByCode", OrdStatusCd.class)
                .setParameter("code", "PRCS").getSingleResult();

        query = getEntityManager().createQuery(
                "SELECT COUNT(o) FROM OrderHistory o WHERE o.depUidFk = :departmentId AND o.ordStatusCd = :code",
                Long.class).setParameter("departmentId", department).setParameter("code", ordStatusCd);
        count = query.getSingleResult();

        items = new ArrayList<>(1);
        orderStatusData = new OrderStatusData();
        orderStatusData.setName("Processing");
        items.add(count.intValue());
        orderStatusData.setItems(items);
        orderStatusSummaryData.getItems().add(orderStatusData);

        ordStatusCd = getEntityManager().createNamedQuery("OrdStatusCd.findByCode", OrdStatusCd.class)
                .setParameter("code", "SHIP").getSingleResult();

        query = getEntityManager().createQuery(
                "SELECT COUNT(o) FROM OrderHistory o WHERE o.depUidFk = :departmentId AND o.ordStatusCd = :code",
                Long.class).setParameter("departmentId", department).setParameter("code", ordStatusCd);
        count = query.getSingleResult();

        items = new ArrayList<>(1);
        orderStatusData = new OrderStatusData();
        orderStatusData.setName("Shipped");
        items.add(count.intValue());
        orderStatusData.setItems(items);
        orderStatusSummaryData.getItems().add(orderStatusData);

        ordStatusCd = getEntityManager().createNamedQuery("OrdStatusCd.findByCode", OrdStatusCd.class)
                .setParameter("code", "SUBT").getSingleResult();

        query = getEntityManager().createQuery(
                "SELECT COUNT(o) FROM OrderHistory o WHERE o.depUidFk = :departmentId AND o.ordStatusCd = :code",
                Long.class).setParameter("departmentId", department).setParameter("code", ordStatusCd);
        count = query.getSingleResult();

        items = new ArrayList<>(1);
        orderStatusData = new OrderStatusData();
        orderStatusData.setName("Submitted");
        items.add(count.intValue());
        orderStatusData.setItems(items);
        orderStatusSummaryData.getItems().add(orderStatusData);

    } catch (Exception ex) {
        Logger.error(LOG, FormatHelper.getStackTrace(ex));
        throw new DataAccessException(ex, ErrorCode.DATAACCESSERROR);
    }

    return orderStatusSummaryData;

}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * Gets an organisation by its id/*from www. ja  v  a2 s  .c  o  m*/
 * 
 * @param organisation_id
 * @return
 */
public Organisation getOrganisationById(long organisation_id) {
    try {
        TypedQuery<Organisation> query = em.createNamedQuery("getOrganisationById", Organisation.class);
        query.setParameter("organisation_id", organisation_id);
        query.setParameter("deleted", "true");
        Organisation o = null;
        try {
            o = query.getSingleResult();
        } catch (NoResultException e) {
            // o = null;
        }
        return o;
    } catch (Exception ex2) {
        log.error("[getOrganisationById]", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.basic.Fieldmanagment.java

public Fieldlanguagesvalues getFieldByIdAndLanguageByNavi(Long fieldvalues_id, Long language_id) {
    try {//from   ww w .j a v  a  2 s.c o  m
        if (fieldvalues_id == null) {
            return null;
        }

        TypedQuery<Fieldlanguagesvalues> query = em.createQuery(
                "select f from Fieldlanguagesvalues f WHERE f.language_id = :language_id AND f.fieldvalues_id = :fieldvalues_id",
                Fieldlanguagesvalues.class);
        query.setParameter("fieldvalues_id", fieldvalues_id);
        query.setParameter("language_id", language_id);
        Fieldlanguagesvalues flv = null;
        try {
            flv = performReplace(query.getSingleResult());
        } catch (NoResultException ex) {
        }

        FieldLanguage fieldLanguage = fieldLanguageDaoImpl.getFieldLanguageById(language_id);

        log.debug("Getting FieldById for Language " + fieldLanguage.getName());

        // Check for Right To Left Languages
        if (fieldLanguage.getRtl()) {

            log.debug("Language requieres RTL");

            Fieldlanguagesvalues remote = flv;
            Fieldlanguagesvalues toAdd = new Fieldlanguagesvalues();
            toAdd.setFieldlanguagesvalues_id(remote.getFieldlanguagesvalues_id());
            toAdd.setFieldvalues_id(remote.getFieldvalues_id());
            toAdd.setLanguage_id(remote.getLanguage_id());

            String[] splitted = remote.getValue().split(" ");
            String reverseOrder = "";
            for (int i = splitted.length - 1; i >= 0; i--) {
                reverseOrder += splitted[i];
                if (splitted.length != 1) {
                    reverseOrder += " ";
                }
            }
            toAdd.setValue(reverseOrder);

            return toAdd;
        } else {
            log.debug("Language doesnt requiere RTL");

            return flv;
        }

    } catch (Exception ex2) {
        log.error("[getFieldByIdAndLanguage]: ", ex2);
    }
    return null;
}

From source file:de.micromata.genome.jpa.Emgr.java

/**
 * Select single.//from   w  ww  .  j a va 2 s.  c  o  m
 *
 * @param <R> the generic type
 * @param cls the cls
 * @param sql the sql
 * @param keyValues the key values
 * @return the r
 */
@Override
public <R> R selectSingleAttached(final Class<R> cls, final String sql, final Object... keyValues) {
    TypedQuery<R> q = createQuery(cls, sql, keyValues);
    return q.getSingleResult();

}

From source file:de.micromata.genome.jpa.Emgr.java

/**
 * Select single detached.//from   w  w  w  .ja va  2  s  . co  m
 *
 * @param <R> the generic type
 * @param cls the cls
 * @param sql the sql
 * @param keyValues the key values
 * @return the r
 */
@Override
public <R> R selectSingleDetached(final Class<R> cls, final String sql, final Object... keyValues) {

    TypedQuery<R> q = createQuery(cls, sql, keyValues);
    R r = q.getSingleResult();
    detach(r);
    return r;

}