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:org.openmeetings.app.data.calendar.daos.AppointmentDaoImpl.java

public Appointment getAppointmentByIdBackup(Long appointmentId) {
    try {/*from   w  ww. j  a  v  a 2s . co m*/

        String hql = "select a from Appointment a " + "WHERE a.appointmentId = :appointmentId ";

        TypedQuery<Appointment> query = em.createQuery(hql, Appointment.class);
        query.setParameter("appointmentId", appointmentId);

        Appointment appoint = null;
        try {
            appoint = query.getSingleResult();
        } catch (NoResultException ex) {
        }

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

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

protected String findServiceName(
        final eu.domibus.common.model.org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704.Service service)
        throws EbMS3Exception {
    final String type = service.getType();
    final String value = service.getValue();
    final TypedQuery<String> query;
    if (type == null || type.isEmpty()) {
        try {/*from   w ww.j  a va2 s  .c  o  m*/
            URI.create(value); //if not an URI an IllegalArgumentException will be thrown
            query = entityManager.createNamedQuery("Service.findWithoutType", String.class);
            query.setParameter("SERVICE", value);
        } catch (final IllegalArgumentException e) {
            final EbMS3Exception ex = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0003, e, null);
            ex.setErrorDetail("Service " + value + " is not a valid URI [CORE] 5.2.2.8");
            throw ex;
        }
    } else {
        query = this.entityManager.createNamedQuery("Service.findByServiceAndType", String.class);
        query.setParameter("SERVICE", value);
        query.setParameter("TYPE", type);
    }
    try {
        return query.getSingleResult();
    } catch (final NoResultException e) {
        PModeDao.LOG.info("No machting service found", e);
        throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0001, "No machting service found", null,
                null, null);
    }
}

From source file:org.openmeetings.app.data.calendar.daos.AppointmentDaoImpl.java

public Appointment getAppointmentById(Long appointmentId) {
    try {/*www.j a v  a  2s . com*/

        String hql = "select a from Appointment a " + "WHERE a.deleted <> :deleted "
                + "AND a.appointmentId = :appointmentId ";

        TypedQuery<Appointment> query = em.createQuery(hql, Appointment.class);
        query.setParameter("deleted", "true");
        query.setParameter("appointmentId", appointmentId);

        Appointment appoint = null;
        try {
            appoint = query.getSingleResult();
        } catch (NoResultException ex) {
        }
        return appoint;
    } catch (Exception ex2) {
        log.error("[getAppointmentById]: ", ex2);
    }
    return null;
}

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

@Override
public final <T> T findSingleResult(String queryString, Object[] pParameters, Class<T> entityClass)
        throws UnauthorizedAccessAttemptException {
    T result = null;//from w  w  w .  ja v a 2  s .c o m
    try {
        LOGGER.debug("Building typed query");
        TypedQuery<T> q = entityManager.createQuery(queryString, entityClass);
        applyProperties(q);
        if (pParameters != null && pParameters.length != 0) {
            LOGGER.debug("Adding {} parameters to query", pParameters.length);
            for (int i = 0; i < pParameters.length; i++) {
                q.setParameter(i + 1, pParameters[i]);
            }
        }
        LOGGER.debug("Executing query for single result.");
        result = q.getSingleResult();
        LOGGER.debug("Query returned.");
    } catch (RuntimeException re) {
        LOGGER.warn("Failed to obtain typed single query result", re);
    } finally {
        finalizeEntityManagerAccess("find single result with plain SQL '" + queryString + "'", null,
                entityClass);
    }
    LOGGER.debug("Returning result.");
    return result;
}

From source file:org.openmeetings.app.data.calendar.daos.AppointmentDaoImpl.java

public Appointment getNextAppointment(Date appointmentStarttime) {
    try {//from ww  w.  j a va 2 s . co  m

        String hql = "select a from Appointment a " + "WHERE a.deleted <> :deleted "
                + "AND a.appointmentStarttime > :appointmentStarttime ";

        TypedQuery<Appointment> query = em.createQuery(hql, Appointment.class);
        query.setParameter("deleted", "true");
        query.setParameter("appointmentStarttime", appointmentStarttime);

        Appointment appoint = null;
        try {
            appoint = query.getSingleResult();
        } catch (NoResultException ex) {
        }

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

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

/**
 * Gets the by logical id./*from  ww  w.  j a  v a 2s  .c  o m*/
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:48 PM
 */
@Override
public EndUsers getByLogicalId(Serializable idEntity) {
    TypedQuery<EndUsers> query = entityManager.createNamedQuery(
            EndUsers.class.getSimpleName() + "." + NormalizedEntity.NAMED_QUERY_ID, EndUsers.class);
    query.setParameter("appUsername", ((EndUsersPK) idEntity).getAppUsername());
    return query.getSingleResult();
}

From source file:org.openmeetings.app.data.conference.Invitationmanagement.java

public Invitations getInvitationbyAppointementId(Long invId) {
    log.debug("getInvitationbyId");

    try {//from   w w  w.j av a2s  .  co m
        String hql = "select invi from Invitations invi " + "WHERE invi.deleted <> :deleted "
                + "AND invi.invitations_id = :invid";

        TypedQuery<Invitations> query = em.createQuery(hql, Invitations.class);
        query.setParameter("deleted", "true");
        query.setParameter("invid", invId);

        Invitations inv = null;
        try {
            inv = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return inv;
    } catch (Exception e) {
        log.error("getInvitationsbyId : ", e);
        return null;
    }
}

From source file:org.openmeetings.app.data.conference.Invitationmanagement.java

/**
 * @author becherer/*from ww w.  j  ava 2  s.co  m*/
 * @param invId
 * 
 */
public Invitations getInvitationbyId(Long invId) {
    log.debug("getInvitationbyId");

    try {
        String hql = "select invi from Invitations invi " + "WHERE invi.deleted <> :deleted "
                + "AND invi.invitations_id = :invid";

        TypedQuery<Invitations> query = em.createQuery(hql, Invitations.class);
        query.setParameter("deleted", "true");
        query.setParameter("invid", invId);

        Invitations inv = null;
        try {
            inv = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return inv;
    } catch (Exception e) {
        log.error("getInvitationsbyId : ", e);
        return null;
    }
}

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

/**
 * Gets the by logical id./*from   www .  jav  a2 s  .  c o  m*/
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:39 PM
 */
@Override
public WorkRequestsXEndUsers getByLogicalId(Serializable idEntity) {
    TypedQuery<WorkRequestsXEndUsers> query;
    if (idEntity instanceof WorkRequestsXEndUsersPK) {
        query = entityManager.createNamedQuery("WorkRequestsXEndUsers." + NormalizedEntity.NAMED_QUERY_ID,
                WorkRequestsXEndUsers.class);
        query.setParameter("nameEndUser", ((WorkRequestsXEndUsersPK) idEntity).getNameEndUser());
        query.setParameter("categoryId", ((WorkRequestsXEndUsersPK) idEntity).getCategoryId());
        query.setParameter("idWorkRequest", ((WorkRequestsXEndUsersPK) idEntity).getIdWorkRequest());
    } else {
        query = entityManager.createNamedQuery("WorkRequestsXEndUsers.findByNameEndUser",
                WorkRequestsXEndUsers.class);
        query.setParameter("nameEndUser", ((WorkRequestsXEndUsersPK) idEntity).getNameEndUser());
    }

    return query.getSingleResult();
}

From source file:org.openmeetings.app.data.conference.Invitationmanagement.java

/**
 * /*from ww  w .  j a va 2 s.c  om*/
 * @param hashCode
 * @param hidePass
 * @return
 */
public Object getInvitationByHashCode(String hashCode, boolean hidePass) {
    try {
        String hql = "select c from Invitations as c " + "where c.hash LIKE :hashCode "
                + "AND c.deleted = :deleted";
        TypedQuery<Invitations> query = em.createQuery(hql, Invitations.class);
        query.setParameter("hashCode", hashCode);
        query.setParameter("deleted", "false");
        Invitations invitation = null;
        try {
            invitation = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        if (invitation == null) {
            // already deleted or does not exist
            return new Long(-31);
        } else {
            if (invitation.getCanBeUsedOnlyOneTime()) {

                // do this only if the user tries to get the Invitation, not
                // while checking the PWD
                if (hidePass) {
                    // one-time invitation
                    if (invitation.getInvitationWasUsed()) {
                        // Invitation is of type *only-one-time* and was
                        // already used
                        return new Long(-32);
                    } else {
                        // set to true if this is the first time / a normal
                        // getInvitation-Query
                        invitation.setInvitationWasUsed(true);
                        this.updateInvitation(invitation);
                        // invitation.setInvitationpass(null);
                        invitation.setAllowEntry(true);
                        return invitation;
                    }
                } else {
                    invitation.setAllowEntry(true);
                    return invitation;
                }

            } else if (invitation.getIsValidByTime()) {

                Calendar now = Calendar.getInstance();

                if (invitation.getValidFrom().getTime() <= now.getTime().getTime()
                        && invitation.getValidTo().getTime() >= now.getTime().getTime()) {
                    this.updateInvitation(invitation);
                    // invitation.setInvitationpass(null);
                    invitation.setAllowEntry(true);
                    return invitation;
                } else {

                    // Invitation is of type *period* and is not valid
                    // anymore, this is an extra hook to display the time
                    // correctly
                    // in the method where it shows that the hash code does
                    // not work anymore
                    invitation.setAllowEntry(false);

                    return invitation;
                }
            } else {
                // Invitation is not limited, neither time nor single-usage
                this.updateInvitation(invitation);

                invitation.setAllowEntry(true);
                // invitation.setInvitationpass(null);
                return invitation;
            }
        }

    } catch (Exception err) {
        log.error("[getInvitationByHashCode]", err);
    }
    return new Long(-1);
}