Example usage for javax.persistence.criteria CriteriaBuilder equal

List of usage examples for javax.persistence.criteria CriteriaBuilder equal

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder equal.

Prototype

Predicate equal(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param uuid// w  ww  . ja v a2s.  com
 *            the id of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookById(final String uuid) {
    BookType m = null;
    if (uuid == null) {
        this.logger.debug("getBookById: UUID is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(EntityType_.UUID), uuid));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for id=" + uuid);
            m = null;
        }
    }
    return m;
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}//ww  w .  ja v  a 2 s.  c om
 */
public EntityType findExact(String key, Object value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

    return entityManager.createQuery(query).getSingleResult();
}

From source file:org.sloth.persistence.impl.ReportDaoImpl.java

private Collection<Report> get(boolean processed) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.processed), processed));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} {} Reports.", (processed) ? "processed" : "unprocessed", result.size());
    return result;
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/*from   w  w w  . j  a  va2s . c  om*/
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(FIELD_CONSTANT_ID), id));

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.BasicAbstractGenericDaoImpl.java

/**
 * {@inheritDoc}/* w  w  w .  ja v  a  2 s  . co  m*/
 */
public List<EntityType> findExactList(String key, IDType value, Class<EntityType> entityClass) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get(key), value));

    return entityManager.createQuery(query).getResultList();

}

From source file:com.excilys.ebi.sample.jpa.query.benchmark.repository.impl.JPACriteriaRepository.java

@Override
public List<Song> getSongsBySameArtistOrderBySongTitle(final Integer songId) {

    // select s.artist.songs from Song s where s.id=?

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Song> query = builder.createQuery(Song.class);

    Root<Song> song1 = query.from(Song.class);
    Join<Artist, Song> artistSongs = song1.join(Song_.artist).join(Artist_.songs);

    query.select(artistSongs).where(builder.equal(song1.get(Song_.id), songId))
            .orderBy(builder.asc(artistSongs.get(Song_.title)));
    return em.createQuery(query).getResultList();
}

From source file:net.groupbuy.dao.impl.FriendLinkDaoImpl.java

public List<FriendLink> findList(Type type) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FriendLink> criteriaQuery = criteriaBuilder.createQuery(FriendLink.class);
    Root<FriendLink> root = criteriaQuery.from(FriendLink.class);
    criteriaQuery.select(root);/*from   w w  w  . j a va2  s  .  c o m*/
    if (type != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("type"), type));
    }
    criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

From source file:org.sloth.persistence.impl.ReportDaoImpl.java

@Override
public Collection<Report> getByUser(User u) throws NullPointerException, IllegalArgumentException {
    if (u == null) {
        throw new NullPointerException();
    }//from   ww w.  j a  v  a  2 s .co  m
    if (u.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.author), u));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("Found {} Reports by {}.", result.size(), u);
    return result;
}

From source file:org.wallride.repository.PostRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Post> root = query.from(Post.class);
    query.select(root.get(Post_.id));//from w ww  .j  a  v  a  2 s .c o  m
    query.where(cb.equal(root.get(Post_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:controllers.AuthController.java

@UnitOfWork
@FilterWith(CTCheck.class)
public Result login(Context context, Session session, User req) {
    EntityManager entitymanager = entitiyManagerProvider.get();
    CriteriaBuilder cb = entitymanager.getCriteriaBuilder();

    try {/*from  ww  w .ja  v  a  2s . c o  m*/
        CriteriaQuery<User> query = cb.createQuery(User.class);
        Root<User> a = query.from(User.class);
        query.where(cb.equal(a.get(User_.email), req.getEmail()),
                cb.equal(a.get(User_.password), req.getPassword()));
        User u = entitymanager.createQuery(query).getSingleResult();
        String token = SessionIdentifierGenerator.nextSessionId();
        context.addCookie(Cookie.builder("token", token).build());
        session.put("token", token);
        session.put("email", u.getEmail());
        session.put("id", String.valueOf(u.getId()));
        ninjaCache.set(token, u.getId());
        return Results.json().render(new RespAuth(u.getId(), token, u.getEmail()));
    } catch (Exception e) {
        e.printStackTrace();
        return Results.json().status(400).render(new JSendResp(400, e));
    }
}