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:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

@SuppressWarnings("unchecked")
public List<BunMetadata> readBunsMetadataForOwnerID(Long ownerid, int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    //Query q = entityManager.createQuery("SELECT m FROM BunMetadata m");
    Query q;//from  ww  w. j a  v  a2  s .  com

    if ((ownerid != null) && (ownerid >= 0))
        q = entityManager
                .createQuery("SELECT a FROM BunMetadata a WHERE a.owner.id=" + ownerid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM BunMetadata a ORDER BY a.id");

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

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

@SuppressWarnings("unchecked")
public List<ApplicationMetadata> readAppsMetadataForOwnerID(Long ownerid, int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    //Query q = entityManager.createQuery("SELECT m FROM BunMetadata m");
    Query q;//from   w  w w .j  av  a 2  s.  c  o m

    if ((ownerid != null) && (ownerid >= 0))
        q = entityManager.createQuery(
                "SELECT a FROM ApplicationMetadata a WHERE a.owner.id=" + ownerid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM ApplicationMetadata a ORDER BY a.id");

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:cn.buk.hotel.dao.HotelDaoImpl.java

@Override
@Transactional//from  w ww .  j a  v a2s .  co m
public int createHotelInfo(HotelInfo hotelInfo) {
    int retCode = 0;
    EntityManager em = getEm();
    try {
        List<HotelInfo> hotelInfos = em.createQuery("select o from HotelInfo o where o.hotelCode = :hotelCode")
                .setParameter("hotelCode", hotelInfo.getHotelCode()).getResultList();
        if (hotelInfos.size() > 0) {
            retCode = 2;
        } else {
            em.persist(hotelInfo);
            retCode = 1;
        }
    } catch (Exception ex) {
        retCode = -1;
        logger.info("HotelCode: " + hotelInfo.getHotelCode() + ", HotelName: " + hotelInfo.getHotelName() + ": "
                + ex.getMessage());
    }
    return retCode;
}

From source file:cn.buk.hotel.dao.HotelDaoImpl.java

@Override
@Transactional//from  w  ww  .  j a v a2s .  c  o  m
public int createHotelRatePlans(List<HotelRatePlan> ratePlans) {
    if (ratePlans == null || ratePlans.size() == 0)
        return 0;

    EntityManager em = getEm();

    List<HotelRatePlan> ratePlans0 = em
            .createQuery("select o from HotelRatePlan o where o.hotelInfo = :hotelInfo")
            .setParameter("hotelInfo", ratePlans.get(0).getHotelInfo()).getResultList();

    /**
     * ???RatePlan
     */
    boolean beDeleted;
    for (HotelRatePlan ratePlan0 : ratePlans0) {
        beDeleted = true;
        for (HotelRatePlan ratePlan : ratePlans) {
            if (ratePlan.getRatePlanCode() == ratePlan0.getRatePlanCode()) {
                beDeleted = false;
                break;
            }
        }
        if (beDeleted)
            em.remove(ratePlan0);
    }

    for (HotelRatePlan ratePlan : ratePlans) {
        createHotelRatePlan(ratePlan);
    }

    return 1;
}

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

@SuppressWarnings("unchecked")
public List<BunMetadata> readBunsMetadata(Long categoryid, int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    //Query q = entityManager.createQuery("SELECT m FROM BunMetadata m");
    Query q;/*from  w ww.j ava2  s  . c  o m*/

    if ((categoryid != null) && (categoryid >= 0))
        q = entityManager.createQuery(
                "SELECT a FROM BunMetadata a WHERE a.categories.id=" + categoryid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM BunMetadata a ORDER BY a.id");

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

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

@SuppressWarnings("unchecked")
private CrawledResource sameURLExists(String url, Job persistedJob, EntityManager entityManager) {
    List<CrawledResource> listCR = entityManager.createQuery("from CrawledResource cr where cr.url=:url")
            .setParameter("url", url).getResultList();
    if (listCR.size() == 1) {
        return listCR.get(0);
    }//from w ww.j av  a 2 s  .  c o  m
    return null;
}

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

/**
 * {@inheritDoc}//  ww  w  .  ja  va  2  s  .  c om
 */
@Override
public <T extends BaseEntity> int deleteByCriteria(DeleteCriteria<T> criteria) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        em.getTransaction().begin();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaDelete<T> cd = cb.createCriteriaDelete(criteria.getEntity());
        Root<T> root = cd.from(criteria.getEntity());
        int rowsDeleted = em
                .createQuery(cd.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root))))
                .executeUpdate();
        em.getTransaction().commit();
        LOGGER.debug("deleteByCriteria: No. of rows deleted: [{}]", rowsDeleted);
        return rowsDeleted;
    } catch (Exception ex) { // NOSONAR
        Transactions.markRollback(em);
        throw new JpaException(ex);
    } finally {
        Transactions.rollback(em);
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.xidu.framework.common.dao.impl.BaseDaoImpl.java

/**
 * /*from   w w  w .ja  v  a2  s.  com*/
 * {@inheritDoc} overridden:
 * 
 * @Date : 2011-3-28
 * @see com.xidu.framework.common.dao.IBaseDao#countByQuery(java.lang.String)
 * 
 */
@Override
public int countByQuery(final String queryStr) {

    return getJpaTemplate().execute(new JpaCallback<Integer>() {

        @Override
        public Integer doInJpa(EntityManager em) throws PersistenceException {

            return ((Long) em.createQuery(queryStr).getSingleResult()).intValue();
        }

    });
}

From source file:com.xidu.framework.common.dao.impl.BaseDaoImpl.java

/**
 * /*  ww  w.  jav  a 2  s  . c  om*/
 * {@inheritDoc} overridden:
 * 
 * @Date : 2011-3-28
 * @see com.xidu.framework.common.dao.IBaseDao#countByQueryWithMap(java.lang.String,
 *      java.util.Map)
 * 
 */
@Override
public int countByQueryWithMap(final String queryStr, final Map<String, ?> paraMap) {

    return getJpaTemplate().execute(new JpaCallback<Integer>() {

        @Override
        public Integer doInJpa(EntityManager em) throws PersistenceException {

            Query query = em.createQuery(queryStr);
            prepareQueryParam(query, paraMap);
            return ((Long) query.getSingleResult()).intValue();
        }

    });
}

From source file:com.xidu.framework.common.dao.impl.BaseDaoImpl.java

/**
 * /* w  w w.j av  a 2s.c om*/
 * {@inheritDoc} overridden:
 * 
 * @Date : 2011-3-28
 * @see com.xidu.framework.common.dao.IBaseDao#countByQueryWithVaParam(java.lang.String,
 *      java.lang.Object[])
 * 
 */
@Override
public int countByQueryWithVaParam(final String queryStr, final Object... params) {

    return getJpaTemplate().execute(new JpaCallback<Integer>() {

        @Override
        public Integer doInJpa(EntityManager em) throws PersistenceException {

            Query query = em.createQuery(queryStr);
            prepareQueryParam(query, params);
            return ((Long) query.getSingleResult()).intValue();
        }

    });
}