Example usage for javax.persistence EntityManager createQuery

List of usage examples for javax.persistence EntityManager createQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createQuery.

Prototype

public Query createQuery(CriteriaDelete deleteQuery);

Source Link

Document

Create an instance of Query for executing a criteria delete query.

Usage

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}// w w  w  . j a va  2s  .c  o m
 */
@Override
public <T extends BaseEntity> List<Tuple> findByTupleQuery(TupleQueryCriteria<T> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Tuple> cq = cb.createTupleQuery();
        Root<T> root = cq.from(criteria.getEntity());
        return em
                .createQuery(cq
                        .multiselect(criteria.getSelections().stream().map(root::get).toArray(Selection[]::new))
                        .where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root))))
                .getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:dao.jpa.TestJpaDao.java

@Test
@Transactional//from   ww  w .  j  a v a 2 s. com
public void testCopy() {
    EntityManager em = bookDao.getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Book> criteria = cb.createQuery(Book.class);

    // Fetch join
    Root<Book> root = criteria.from(Book.class);
    Path<String> path = root.join("author").<String>get("name");
    root.fetch("author");
    criteria.select(root);

    // SubQuery
    Subquery<String> sq = criteria.subquery(String.class);
    Root<Author> author = sq.from(Author.class);
    sq.select(author.<String>get("name"));
    sq.where(cb.equal(author.<String>get("name"), "Rod"));

    criteria.where(cb.in(path).value(sq));

    CriteriaQuery<Book> copy = cb.createQuery(Book.class);
    JpaUtils.copyCriteria(criteria, copy);

    List<Book> copyBooks = em.createQuery(copy).getResultList();
    List<Book> books = em.createQuery(criteria).getResultList();
    assertEquals(books, copyBooks);
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*from w w  w.  ja v a2  s .  com*/
 */
@Override
public <T extends BaseEntity> List<T> findByCriteria(ReadCriteria<T> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity());
        Root<T> root = cq.from(criteria.getEntity());
        return em.createQuery(cq.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root))))
                .getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*w ww .  java 2s .  c  o  m*/
 */
@Override
public <T extends BaseEntity> List<T> findPaginatedRecordsByCriteria(ReadCriteria<T> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity());
        Root<T> root = cq.from(criteria.getEntity());
        return em.createQuery(cq.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root))))
                .setFirstResult(criteria.getStartPos()).setMaxResults(criteria.getMaxResult()).getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*  ww  w  .  j a v a 2 s.c o m*/
 */
@Override
public <T extends BaseEntity> List<T> findByINOperator(Class<T> entity, String attributeName,
        List<Object> values) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaQuery<T> cq = em.getCriteriaBuilder().createQuery(entity);
        Root<T> root = cq.from(entity);
        Predicate predicate = root.get(attributeName).in(values);
        return em.createQuery(cq.select(root).where(predicate)).getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/* w w  w .  jav a2s .co m*/
 */
@Override
public <T extends BaseEntity, C> List<C> findByCriteriaAndMapConstructor(ConstructorCriteria<T, C> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<C> cq = cb.createQuery(criteria.getConstructorClass());
        Root<T> root = cq.from(criteria.getEntity());
        return em.createQuery(cq
                .select(cb.construct(criteria.getConstructorClass(),
                        criteria.getSelections().stream().map(root::get).toArray(Selection[]::new)))
                .where(Predicates.from(criteria.getCriteriaAttributes(), cb, root))).getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteAllInstalledBuns() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*  w  ww .  j  a va2s .  c om*/

    Query q = entityManager.createQuery("DELETE FROM InstalledBun ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteAllUsers() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from   w w  w.  ja va  2s  .co  m

    Query q = entityManager.createQuery("DELETE FROM BakerUser ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteAllSubscribedResources() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//  w  w  w  .jav a 2s .  co m

    Query q = entityManager.createQuery("DELETE FROM SubscribedResource ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:it.drwolf.ridire.session.async.Mapper.java

private boolean isResourceAlreadyMapped(String digest, String url, EntityManager entityManager) {
    boolean ret = entityManager.createQuery("from CrawledResource cr where cr.digest=:digest or cr.url=:url")
            .setParameter("digest", digest).setParameter("url", url).getResultList().size() > 0 ? true : false;
    return ret;//w  w w. ja v a  2s. c  om
}