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:es.ucm.fdi.dalgs.course.repository.CourseRepository.java

public Course getCourse(Long id_course, Long id_academic) {
    AcademicTerm academic = em.getReference(AcademicTerm.class, id_academic);
    Query query = em.createQuery("select c from Course c where c.academicTerm=?1 and c.id=?2");
    query.setParameter(1, academic);/* ww  w. j a  v a 2s.c o  m*/
    query.setParameter(2, id_course);

    if (query.getResultList().isEmpty())
        return null;
    else
        return (Course) query.getSingleResult();
}

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

@Transactional(readOnly = true)
public UserBean getUser(String userName) {
    Query queryGetUser = em.createNamedQuery("getUser");
    queryGetUser.setParameter("username", userName);
    UserEntity ue = (UserEntity) queryGetUser.getSingleResult();
    return new UserBean(ue);
}

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

@Transactional(readOnly = true)
public boolean isPasswordChangeNeeded(String userName) {
    Query queryGetUser = em.createNamedQuery("getUser");
    queryGetUser.setParameter("username", userName);
    UserEntity ue = (UserEntity) queryGetUser.getSingleResult();
    if (ue.getPasswordChangeNeeded() == UserEntity.PASSWORD_CHANGE_NEEDED) {
        return true;
    } else {/*  w  ww .  j  a  va  2 s.  c  o  m*/
        return false;
    }
}

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

@Transactional
public UserBean resetPassword(String userName, RequestContext ctx) {
    UserBean ub = null;//from   w  w  w .  j av a  2 s.  com
    Query queryGetUser = em.createNamedQuery("getUser");
    queryGetUser.setParameter("username", userName);
    UserEntity ue = (UserEntity) queryGetUser.getSingleResult();
    // form password
    String newPassword = RandomStringUtils.random(8, true, true);
    UserEntity ue1 = em.find(UserEntity.class, ue.getId());
    ue1.setPassword(passwordEncoder.getHash(ue.getUsername(), newPassword));
    ue1.setPasswordChangeNeeded(UserEntity.PASSWORD_CHANGE_NEEDED);
    em.merge(ue1);
    ub = new UserBean(ue1);
    ub.setPassword(newPassword);
    // Add message to be displayed on UI
    ctx.getMessageContext()
            .addMessage(new MessageBuilder().info().defaultText("Password was reset succesfuly.").build());
    return ub;
}

From source file:gwap.rest.PastTerminaSessionBean.java

public void setTermName(String termName) {
    Query q = entityManager.createNamedQuery("term.byName");
    q.setParameter("termName", termName);
    Term t = (Term) q.getSingleResult();

    this.term = t;
}

From source file:com.assetmanager.service.auth.UserDetailsServiceImpl.java

/**
 * Activates the user with the given activation key.
 *
 * @param key the activation key//  w w w  .j  a va  2 s  . c o  m
 * @return true if successful; false otherwise
 */
@Override
@Transactional
public final boolean activateUser(final String key) {
    final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE activationKey = :key");
    query.setParameter("key", key);

    try {
        final UserAccount user = (UserAccount) query.getSingleResult();
        user.setEnabled(true);

        entityManager.persist(user);

        memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));

        return true;
    } catch (NoResultException e) {
        return false;
    }
}

From source file:es.ucm.fdi.dalgs.course.repository.CourseRepository.java

public Course exist(Course course) {
    Query query = em.createQuery("select c from Course c  where c.academicTerm=?1 and c.subject=?2");
    query.setParameter(1, course.getAcademicTerm());
    query.setParameter(2, course.getSubject());

    if (query.getResultList().isEmpty())
        return null;
    else/*from  w  ww. j  a v  a 2  s . c  o  m*/
        return (Course) query.getSingleResult();
}

From source file:es.ucm.fdi.dalgs.course.repository.CourseRepository.java

public Long isDisabled(Long id_academic, Long id_subject) {
    Subject subject = em.getReference(Subject.class, id_subject);
    AcademicTerm academic = em.getReference(AcademicTerm.class, id_academic);

    Query query = em
            .createQuery("select c from Course c where c.subject=?1 and c.academicTerm=?2 and c.isDeleted=1");
    query.setParameter(1, subject);//  ww  w. jav a 2 s.  c om
    query.setParameter(2, academic);

    if (query.getResultList().isEmpty())
        return null;

    Course aux = (Course) query.getSingleResult();
    return aux.getId();
}

From source file:BO.UserHandler.java

public boolean login(VUser u) {

    EntityManager em;/*w  w w .  j av  a 2 s .  c o  m*/
    EntityManagerFactory emf;
    emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    em = emf.createEntityManager();

    try {

        Query q = em.createQuery("SELECT u FROM User u WHERE u.email LIKE :email");
        q.setParameter("email", u.getEmail());
        q.setMaxResults(1);
        q.getSingleResult();
        return true;
    } catch (NoResultException e) {
        em.getTransaction().begin();
        User userToInsert = new User(u.getEmail());
        em.persist(userToInsert);
        em.flush();
        em.getTransaction().commit();
        return true;
    } finally {
        if (em != null) {
            em.close();
        }
        emf.close();
    }
}

From source file:com.assetmanager.service.auth.UserDetailsServiceImpl.java

/**
 * Returns the user account for the given username.
 *
 * @param username the username//from  w ww . j a v  a2  s.  c  o m
 * @return the user account
 */
@Override
public final UserAccount getUser(final String username) {
    UserAccount user = (UserAccount) memcacheService.get(username);

    if (user == null) {
        final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
        query.setParameter(USERNAME, username);

        try {
            user = (UserAccount) query.getSingleResult();

            memcacheService.put(username, user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
        } catch (NoResultException e) {
            return null;
        }
    }

    return user;
}