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.assetmanager.service.auth.UserDetailsServiceImpl.java

/**
 * Locates the user based on the username.
 *
 * @param username string the username/*from ww  w .  j a v  a2s  .c  o  m*/
 * @return the user details
 */
@Override
public final UserDetails loadUserByUsername(final String username) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    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) {
            throw new UsernameNotFoundException("Username not found.", e);
        }
    }

    authorities.add(new SimpleGrantedAuthority(user.getRole()));

    return new EnhancedUser(user.getUsername(), user.getEmail(), user.getDisplayName(), user.getPassword(),
            user.getSalt(), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(),
            user.isAccountNonLocked(), authorities);
}

From source file:com.telefonica.euro_iaas.paasmanager.dao.impl.NetworkInstanceDaoJpaImpl.java

private NetworkInstance findByNetworkInstanceName(String name, String vdc, String region)
        throws EntityNotFoundException {
    Query query = getEntityManager().createQuery("select p from NetworkInstance p left join "
            + "fetch p.subNets where p.name = :name and p.vdc = :vdc and p.region = :region");
    query.setParameter("name", name);
    query.setParameter("vdc", vdc);
    query.setParameter("region", region);
    NetworkInstance networkInstance = null;
    try {//from   w w  w.j  ava  2 s  .co m
        networkInstance = (NetworkInstance) query.getSingleResult();
    } catch (NoResultException e) {
        String message = " No NetworkInstance found in the database with id: " + name + " vdc " + vdc
                + " region " + region + " Exception: "

                + e.getMessage();
        log.debug(message);
        throw new EntityNotFoundException(NetworkInstance.class, "name", name);
    }
    return networkInstance;
}

From source file:org.venice.piazza.common.hibernate.dao.job.JobDaoImpl.java

public Page<JobEntity> getJobList(Pagination pagination) {
    // Query/*w ww .j av a2  s.  com*/
    String queryString = String.format(JOB_QUERY, Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, JobEntity.class);
    query.setParameter(1, pagination.getSortBy());
    query.setParameter(2, pagination.getPerPage());
    query.setParameter(3, pagination.getPage() * pagination.getPerPage());
    List<JobEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(JOB_QUERY_COUNT);
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<JobEntity>(results, null, count);
}

From source file:es.ucm.fdi.dalgs.competence.repository.CompetenceRepository.java

public Competence getCompetence(Long id, Long id_degree) {
    Query query = null;

    Degree degree = em.getReference(Degree.class, id_degree);
    query = em.createQuery("select c from Competence c where c.id=?1 and c.degree=?2 ");
    query.setParameter(1, id);//from  w w  w .  j a  v  a2  s  .  co m
    query.setParameter(2, degree);

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

    return (Competence) query.getSingleResult();

}

From source file:es.ucm.fdi.dalgs.topic.repository.TopicRepository.java

public Topic getTopic(Long id, Long id_module, Long id_degree) {
    Module module = em.getReference(Module.class, id_module);
    Degree degree = em.getReference(Degree.class, id_degree);
    Query query = em
            .createQuery("select t from Topic t where t.id=?1 and t.module = ?2 and t.module.degree=?3");
    query.setParameter(1, id);/*w  ww .j  ava 2s .  com*/
    query.setParameter(2, module);
    query.setParameter(3, degree);

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

From source file:org.apache.cxf.fediz.service.idp.service.jpa.IdpDAOJPAImpl.java

@Override
public Idp getIdp(String realm, List<String> expandList) {
    Query query = null;
    query = em.createQuery("select i from IDP i where i.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    Object idpObj = query.getSingleResult();
    return entity2domain((IdpEntity) idpObj, expandList);
}

From source file:in.bookmylab.jpa.JpaDAO.java

/**
* @param em/*ww w.j av a 2s.  co m*/
* @param user
*/
private void grantDefaultRoleToUser(EntityManager em, User user) {
    Query q = em.createNamedQuery("Role.findByRole");
    q.setParameter("role", RoleName.USER.name());
    Role r = (Role) q.getSingleResult();
    if (user.roleList == null)
        user.roleList = new ArrayList<>();
    user.roleList.add(r);
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.IdpDAOJPAImpl.java

@Override
public void deleteIdp(String realm) {
    Query query = null;
    query = em.createQuery("select i from IDP i where i.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    Object idpObj = query.getSingleResult();
    em.remove(idpObj);/*  w ww.  j ava2 s  .  co m*/

    LOG.debug("IDP '{}' deleted", realm);
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Count articles./*from www.j  av a  2  s .co m*/
 *
 * @return the long
 * @throws Exception the exception
 */
private long countArticles() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.headline) FROM news p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Count categories.//from  ww w . j a  va2  s .  c om
 *
 * @return the long
 * @throws Exception the exception
 */
private long countCategories() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.fs_id) FROM category p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}