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:org.kuali.mobility.xsl.dao.XslDaoImpl.java

public Xsl findXslById(Long id) {
    Query query = entityManager.createQuery("select x from Xsl x where x.xslId = :id");
    query.setParameter("id", id);
    return (Xsl) query.getSingleResult();
}

From source file:com.iselect.kernal.account.dao.UserDaoExtImpl.java

@Override
public UserModelImpl getByUserName(String username) {
    Query query = em.createNamedQuery(UserModelImpl.GET_BY_USERNAME, UserModelImpl.class);
    query.setParameter(UserModelImpl.PARAM_USERNAME, username);
    return (UserModelImpl) query.getSingleResult();
}

From source file:bc8.movies.dao.ActorDaoImpl.java

public Actor getActor(String actorName) {
    Query query = em.createQuery("select actor from Actor actor where actor.name= :actorName");
    query.setParameter("actorName", actorName);
    return (Actor) query.getSingleResult();
}

From source file:com.expressui.sample.dao.RoleDao.java

public Role findByName(String name) {
    Query query = getEntityManager().createQuery("SELECT r FROM Role r WHERE r.name = :name");

    query.setParameter("name", name);

    return (Role) query.getSingleResult();
}

From source file:org.kuali.mobility.xsl.dao.XslDaoImpl.java

public Xsl findXslByCode(String code) {
    Query query = entityManager.createQuery("select x from Xsl x where x.code = :code");
    query.setParameter("code", code);
    return (Xsl) query.getSingleResult();
}

From source file:bc8.movies.dao.MovieDaoImpl.java

public Movie getMovie(String movieName) {
    Query query = em.createQuery("select movie from Movie movie where movie.name= :movieName");

    query.setParameter("movieName", movieName);

    return (Movie) query.getSingleResult();
}

From source file:gumga.framework.application.GumgaCrudAndQueryNotOnlyTypedRepositoryImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from   w  w  w  . j  av  a 2s  .c om*/
public <E> E getUniqueResult(String hql, Class<E> type, Map<String, Object> param) {
    javax.persistence.Query query = entityManager.createQuery(hql);
    query.setMaxResults(1);
    addParam(query, param);
    return type.cast(query.getSingleResult());
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static Run loadRunWithRuntimeParameters(String uuid) throws ConfigurationException, ValidationException {
    EntityManager em = PersistenceUtil.createEntityManager();
    Query q = createNamedQuery(em, "runWithRuntimeParameters");
    q.setParameter("uuid", uuid);
    Run run = (Run) q.getSingleResult();
    em.close();// w  w w  .  j a v  a  2 s. co m
    return run;
}

From source file:com.cloudbees.demo.beesshop.service.BeesShopInitializer.java

@Override
public void afterPropertiesSet() {

    TransactionStatus status = transactionManager
            .getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
    try {//from   w ww . ja v  a2s.  c  o m
        Query query = em.createQuery("select count(p) from Product p");
        long count = (Long) query.getSingleResult();
        if (count == 0) {

            logger.info("Database is empty, insert demo products");

            em.persist(new Product().withName("Buckwheat Honey - One pint").withDescription(
                    "Dark, full-bodied honey from New York state. Mildly sweet, with flavors of molasses, caramel, and smoke. Buckwheat honey is high in minerals and antioxidant compounds, and rumored to help ease coughing from upper respiratory infections.")
                    .withPriceInCents(1199).withProductUrl("http://www.huney.net/gourmet-honey/")
                    .withPhotoUrl("/img/buckwheat-honey.jpg")
                    .withPhotoCredit("http://huneynet.fatcow.com/store/media/BuckwheatHoney.jpg")
                    .withTags("honey"));

            em.persist(new Product().withName("Cotton Honey - One Pint").withDescription(
                    "Cotton honey from west Texas. This unique honey is naturally crystallized and spreadable like butter. It is very sweet with a mild creamy flavor and a clean, fresh smell. Our cotton honey is unstrained and contains pollen and small flecks of beeswax. ")
                    .withPriceInCents(1199).withProductUrl("http://www.huney.net/gourmet-honey/")
                    .withPhotoUrl("/img/cotton-honey.jpg")
                    .withPhotoCredit("http://huneynet.fatcow.com/store/media/West_Texas_Cotton.jpg")
                    .withTags("honey"));

            em.persist(new Product().withName("Honey Shot- 5g sachet Eucalyptus Honey (100%)").withDescription(
                    "Honey 'Shots' is a product developed on the basis of an athletes needs to maintain energy levels when training it then moved to mountain biking as cyclists wanted an alternative to synthesised sports energy bars and sweets. ...")
                    .withPriceInCents(100).withPhotoUrl("/img/Sports-Energy-Shot-web.png")
                    .withPhotoCredit(
                            "http://www.thehivehoneyshop.co.uk/product_images/thumbs/t_Sports-Energy-Shot-web.jpg")
                    .withProductUrl("http://www.thehivehoneyshop.co.uk/itemdetail.asp?itemid=770")
                    .withTags("honey", "beverage"));

        } else {
            logger.info("Products found in the database, don't insert new ones");
        }
        transactionManager.commit(status);
    } catch (RuntimeException e) {
        try {
            transactionManager.rollback(status);
        } catch (Exception rollbackException) {
            logger.warn("Silently ignore", rollbackException);
        }
        throw e;
    }
}

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

@RequestMapping(value = "/changepassword/update", method = RequestMethod.POST)
public String update(@ModelAttribute("changePasswordForm") ChangePasswordForm form, BindingResult result,
        HttpServletRequest request) {/*from  w w w .  j  a va 2  s .co  m*/
    validator.validate(form, result);
    if (result.hasErrors()) {
        return "changepassword/index"; // back to form
    } else {
        if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();
            String newPassword = form.getNewPassword();
            Query query = User.findUsersByEmailAddress(userDetails.getUsername(), null, null);
            User person = (User) query.getSingleResult();
            person.setPassword(messageDigestPasswordEncoder.encodePassword(newPassword, null));
            person.merge();
            logService.logActionAuth(LogService.AUTH_PASSWORD_CHANGED, userDetails.getUsername(),
                    request.getRemoteAddr());
            return "changepassword/thanks";
        } else {
            return "login";
        }
    }
}