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:com.ushahidi.swiftriver.core.api.dao.impl.JpaUserDao.java

public User findByUsernameOrEmail(String search) {
    String qlString = "FROM User u WHERE email = :email OR username = :username";
    TypedQuery<User> query = em.createQuery(qlString, User.class);
    query.setParameter("email", search);
    query.setParameter("username", search);

    User user = null;/*from   w  ww.j a  v  a 2 s  . c om*/
    try {
        user = query.getSingleResult();
    } catch (NoResultException e) {
        logger.info("No user with email {} or username {} exists", search, search);
    }

    return user;
}

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

public Naviglobal getGlobalMenuEntry(long globalId) {
    try {/* w  w w .java2s .co  m*/
        TypedQuery<Naviglobal> query = em.createNamedQuery("getNavigationById", Naviglobal.class);
        query.setParameter("global_id", globalId);
        return query.getSingleResult();
    } catch (Exception ex2) {
        log.error("getGlobalMenuEntry", ex2);
    }
    return null;

}

From source file:com.sshdemo.common.schedule.generate.job.report.dao.EwcmsJobReportDAO.java

public EwcmsJobReport findJobReportByReportId(final Long reportId, final String reportType) {
    String hql = "Select o From EwcmsJobReport o Inner Join ";
    if (reportType.equals("text")) {
        hql += " o.textReport c ";
    } else if (reportType.equals("chart")) {
        hql += " o.chartReport c ";
    }/*from   www.  ja  va  2  s .  c  om*/
    hql += " Where c.id=:reportId ";

    TypedQuery<EwcmsJobReport> query = this.getEntityManager().createQuery(hql, EwcmsJobReport.class);
    query.setParameter("reportId", reportId);

    EwcmsJobReport ewcmsJobReport = null;
    try {
        ewcmsJobReport = (EwcmsJobReport) query.getSingleResult();
    } catch (NoResultException e) {

    }
    return ewcmsJobReport;
}

From source file:br.com.semanticwot.cd.daos.SwotApplicationDAO.java

@Override
public SwotApplication findOne(SystemUser systemUser) {
    TypedQuery<SwotApplication> query = entityManager
            .createQuery("select distinct(p) from SwotApplication " + "p where p.systemUser=:login",
                    SwotApplication.class)
            .setParameter("login", systemUser);
    return query.getSingleResult();

}

From source file:com.ewcms.content.particular.dao.FrontEnterpriseBasicDAO.java

public EnterpriseBasic findEnterpriseBasicByYyzzzch(final String yyzzzch) {
    String hql = "From EnterpriseBasic As p Where p.yyzzzch=:yyzzzch and p.release=true ";
    TypedQuery<EnterpriseBasic> query = this.getEntityManager().createQuery(hql, EnterpriseBasic.class);
    query.setParameter("yyzzzch", yyzzzch);
    EnterpriseBasic enterpriseBasic = null;
    try {/*from ww  w .  j a va  2 s  .co  m*/
        enterpriseBasic = (EnterpriseBasic) query.getSingleResult();
    } catch (NoResultException e) {
    }
    return enterpriseBasic;
}

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

/**
 * Find last./*w  ww . j a  v  a  2s .c  o  m*/
 * 
 * @param dbAccess
 *            the db access
 * @param target
 *            the target
 * @param newRecord
 *            the new record
 * @return the entity
 * @since 28/08/2014, 11:20:27 PM
 */
@Transactional(propagation = Propagation.REQUIRED)
protected Entity findLast(EntityManager dbAccess, Class<Entity> target, Entity newRecord) {
    TypedQuery<Entity> query = dbAccess.createNamedQuery(newRecord.getNamedQueryId(), target);
    return query.getSingleResult();
}

From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the total number of calls for that date.
 *
 * @param userName//from  w ww  .  j  a  va2 s.  c o  m
 *            the user name.
 * @param serviceName
 *            the service name.
 * @param operationName
 *            the operation name.
 * @param date
 *            the date the calls are recorded.
 * @return the number of calls.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public int getCurrentRequestNumber(final String userName, final String serviceName, final String operationName,
        final Date date) {
    int requests = 0;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<SystemUserLogType> q = cb.createQuery(SystemUserLogType.class);
    Root<SystemUserLogType> c = q.from(SystemUserLogType.class);
    q.where(cb.equal(c.<String>get(SystemUserLogType_.context), this.contextNameProvider.getContextName()),
            cb.equal(c.<String>get(SystemUserLogType_.userName), userName),
            cb.equal(c.<String>get(SystemUserLogType_.serviceName), serviceName),
            cb.equal(c.<String>get(SystemUserLogType_.operationName), operationName),
            cb.equal(c.<Date>get(SystemUserLogType_.logDateItem), date));

    TypedQuery<SystemUserLogType> typedQuery = this.em.createQuery(q);
    try {
        SystemUserLogType log = typedQuery.getSingleResult();
        requests = log.getRequestsGranted();
    } catch (Exception e) {
        requests = 0;
    }
    return requests;
}

From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java

@Override
public Long fetchNumberOfMemberRegistersWithoutTerms() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<MemberRegister> memberRegister = cq.from(MemberRegister.class);
    Subquery<Long> sq = cq.subquery(Long.class);
    Root<EvaluationAnswer> evaluationAnswer = sq.from(EvaluationAnswer.class);
    Join<EvaluationAnswer, QuestionChoicesDef> choiceDef = evaluationAnswer.join(EvaluationAnswer_.answer);
    Join<QuestionChoicesDef, EvaluationQuestionDef> questionDef = choiceDef
            .join(QuestionChoicesDef_.evaluationQuestionDef);
    ParameterExpression<QuestionnaireType> questionnaireType = cb.parameter(QuestionnaireType.class,
            "questionnaireType");
    sq.select(evaluationAnswer.get("memberRegister").get("id"))
            .where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireType));
    cq.select(cb.count(memberRegister.get("id"))).where(cb.not(cb.in(memberRegister.get("id")).value(sq)));
    TypedQuery<Long> query = entityManager.createQuery(cq);
    query.setParameter("questionnaireType", QuestionnaireType.TERMS_OF_CONDITION);
    return query.getSingleResult();
}

From source file:fr.univrouen.poste.web.ForgotPasswordController.java

@RequestMapping(value = "/forgotpassword/change", method = RequestMethod.POST)
public String modifyPasswordWithActivationKey(
        @ModelAttribute("changePasswordForm") ForgotChangePasswordForm form, BindingResult result,
        HttpServletRequest request) {//  ww  w . j  av a 2  s  .  co  m
    validator.validate(form, result);
    if (result.hasErrors()) {
        return "changepassword/index"; // back to form
    }
    TypedQuery<User> userQuery = User.findUsersByActivationKey(form.getActivationKey());
    if (null != userQuery && !userQuery.getResultList().isEmpty()) {
        User user = userQuery.getSingleResult();
        user.setPassword(messageDigestPasswordEncoder.encodePassword(form.getNewPassword(), null));
        user.setActivationKey(null);
        user.merge();
    }
    return "redirect:/";
}

From source file:top.knos.user.services.JpaDaoImpl.java

public UserDetails loadUserByEmail(String email) throws UsernameNotFoundException {
    TypedQuery<User> tq = em.createQuery(usersByEmailQuery, User.class).setParameter(1, email);
    UserDetails user = tq.getSingleResult();
    return fillAuthority(user);
}