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:com.iselect.kernal.geo.dao.CountryDaoImpl.java

@Override
public CountryModel getCountryByCode(String code) throws EntityNotFoundException {
    CountryModel country = null;/*from w  w w.  j  ava  2  s .c o m*/
    Query query = em.createNamedQuery("country.findByCode", CountryModelImpl.class);
    query.setParameter("countryCode", code);
    try {
        country = (CountryModel) query.getSingleResult();
    } catch (NoResultException nre) {
        throw new EntityNotFoundException(code, nre);
    }
    return country;
}

From source file:es.ucm.fdi.dalgs.user.repository.UserRepository.java

public Integer numberOfPages() {

    Query query = em.createNativeQuery("select count(*) from user");
    logger.info(query.getSingleResult().toString());
    double dou = Double.parseDouble(query.getSingleResult().toString()) / ((double) noOfRecords);
    return (int) Math.ceil(dou);

}

From source file:org.jrecruiter.dao.jpa.RoleDaoJpa.java

public Role getRole(final String roleName) {

    final Role role;

    javax.persistence.Query query = entityManager.createQuery("select r from Role r " + "where r.name = :role");
    query.setParameter("role", roleName);

    try {/*  ww w.  j  a  v a  2 s .c o  m*/
        role = (Role) query.getSingleResult();
    } catch (NoResultException e) {
        throw new IllegalStateException(String.format(
                "Role '%s' does not exist. Roles should always " + "be there. Is the seed data populated?",
                roleName));
    }

    return role;
}

From source file:org.kuali.mobility.user.dao.UserDaoImpl.java

public User findUserByDeviceId(String deviceId) {
    Query query = entityManager.createQuery("select u from User u where u.deviceId = :deviceId");
    query.setParameter("deviceId", deviceId);
    try {//from  www .j  av a  2 s .  c o m
        return (User) query.getSingleResult();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.healthcit.cacure.dao.ModuleDao.java

public BaseModule getByUUID(String uuid) {
    BaseModule module = null;//  w ww  . j av  a  2s. c  om
    Query query = em.createQuery("from BaseModule fe where uuid = :Id");
    query.setParameter("Id", uuid);
    try {
        module = (BaseModule) query.getSingleResult();
    } catch (javax.persistence.NoResultException e) {
        logger.debug("No object found with uuid " + uuid);
    }
    return module;
}

From source file:com.sapito.db.dao.AbstractDao.java

public int count() {
    javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(entityManager.getCriteriaBuilder().count(rt));
    javax.persistence.Query q = entityManager.createQuery(cq);
    return Integer.valueOf(q.getSingleResult().toString());
}

From source file:DAO.CommentairesDAOImpl.java

@Transactional(readOnly = true)
@Override/* ww w  . ja v a2s . c o  m*/
public CommentairesEntity find(int id) {
    Query q = em.createQuery("SELECT c FROM CommentairesEntity c where c.id = ?");
    q.setParameter(1, id);
    try {
        return (CommentairesEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.june.app.user.repository.jpa.UserRepositoryImpl.java

@Override
public Long selectUserId(String userId) {
    Query query = this.em
            .createQuery("SELECT count(userInfo) FROM UserInfo userInfo WHERE userInfo.userId =:userId");
    query.setParameter("userId", userId);
    return (Long) query.getSingleResult();
}

From source file:com.pet.demo.repository.jpa.JpaOwnerRepositoryImpl.java

public Owner findById(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em
            .createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}

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

@Override
public void validate(Object target, Errors errors) {
    ChangePasswordForm form = (ChangePasswordForm) target;

    try {/*  w w  w.j a  v a  2s.  com*/
        if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();
            Query query = User.findUsersByEmailAddress(userDetails.getUsername(), null, null);
            if (null != query) {
                User person = (User) query.getSingleResult();
                String storedPassword = person.getPassword();
                String currentPassword = form.getOldPassword();
                if (!messageDigestPasswordEncoder.isPasswordValid(storedPassword, currentPassword, null)) {
                    errors.rejectValue("oldPassword", "changepassword.invalidpassword");
                }
                String newPassword = form.getNewPassword();
                String newPasswordAgain = form.getNewPasswordAgain();
                if (!newPassword.equals(newPasswordAgain)) {
                    errors.reject("changepassword.passwordsnomatch");
                }
            }
        }
    } catch (EntityNotFoundException e) {
        errors.rejectValue("emailAddress", "changepassword.invalidemailaddress");
    } catch (NonUniqueResultException e) {
        errors.rejectValue("emailAddress", "changepassword.duplicateemailaddress");
    }
}