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.espirit.moddev.examples.uxbridge.newswidget.jpa.ArticleHandler.java

/**
 * Add or update a news article in the db
 *
 * @param entity The NewsItem//from  w  w w .ja  v a2  s  . com
 */
public void add(UXBEntity entity) throws Exception {

    Article art = buildArticle(entity);

    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();

        Query query = em.createQuery(new StringBuilder()
                .append("SELECT x FROM article x WHERE x.aid = :fsid AND x.language=:language").toString());
        query.setParameter("fsid", art.getAid());
        query.setParameter("language", art.getLanguage());

        /*
        * If the item exists in the db, we update the content and the title of the existing item
        */
        if (query.getResultList().size() > 0) {
            Article db = (Article) query.getSingleResult();
            db.setContent(art.getContent());
            db.setTitle(art.getTitle());
            db.setUrl(art.getUrl());
            db.setCreated(art.getCreated());
            db.setAid(art.getAid());
            db.setLanguage(art.getLanguage());
            db.setVersion(art.getVersion());
            db.setLastmodified(art.getLastmodified());

            art = db;
        }
        // save to db
        em.persist(art);
        em.flush();

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.setRollbackOnly();
            throw e;
        }
        logger.error("Failure while writing to the database", e);
    } finally {
        if (tx != null && tx.isActive()) {
            if (tx.getRollbackOnly()) {
                tx.rollback();
            }
        }
        if (em != null) {
            em.close();
        }
    }
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

/**
 * Gets a value indicating whether the provided token has been previously seen or not.
 * @param em the {@link EntityManager} to use.
 * @param builder the {@link LexiconBuilderDocumentStore} to use.
 * @param token the token to look for.//from   w  w  w  .  j a  v a 2  s . c  o  m
 * @return {@code true} if the token was seen; {@code false} otherwise.
 */
public boolean isSeenToken(EntityManager em, LexiconBuilderDocumentStore builder, String token) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(builder, CannedMessages.NULL_ARGUMENT, "builder");

    Query query = em.createQuery(
            "SELECT doc FROM LexiconDocument doc " + "WHERE doc.store=:builder AND doc.title=:token");
    query.setMaxResults(1).setParameter("builder", builder).setParameter("token", token);

    return query.getResultList().size() > 0;
}

From source file:fr.amapj.service.services.meslivraisons.MesLivraisonsService.java

/**
 * Retourne la liste des mois de cette pages mes livraisons
 * @param dto//from   w w w .ja va 2  s.c o  m
 * @return
 */
private List<Date> getMonth(EntityManager em, Date dateDebut, Date dateFin) {
    // On extrait toutes les livraisons sur l'intervalle et on en dduit la liste de mois
    Query q = em.createQuery("select distinct(mcd.dateLiv) from ModeleContratDate mcd WHERE "
            + "mcd.dateLiv>=:deb AND " + "mcd.dateLiv<=:fin " + "order by mcd.dateLiv");
    q.setParameter("deb", dateDebut, TemporalType.DATE);
    q.setParameter("fin", dateFin, TemporalType.DATE);

    List<Date> mcds = q.getResultList();

    List<Date> res = new ArrayList<Date>();

    for (Date mcd : mcds) {
        Date month = fr.amapj.common.DateUtils.firstDayInMonth(mcd);
        if (res.contains(month) == false) {
            res.add(month);
        }
    }
    return res;
}

From source file:edu.vt.middleware.gator.JpaConfigManager.java

/** {@inheritDoc}. */
@SuppressWarnings("unchecked")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public <T extends Config> List<T> findAll(final Class<T> type) {
    final EntityManager em = getEntityManager();
    final String queryString = String.format("SELECT t FROM %s t", type.getSimpleName());
    logger.trace("Executing query " + queryString);
    return em.createQuery(queryString).getResultList();
}

From source file:streaming.test.StreamingTest.java

public void requete1() {
    System.out.println("Requete 1");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery("SELECT f FROM Film f ORDER BY f.anneeProd ASC ").getResultList());
}

From source file:streaming.test.StreamingTest.java

public void requete7() {
    System.out.println("Requete 7");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery("SELECT f FROM Film f WHERE f.genre.nom <> 'Action'").getResultList());
}

From source file:streaming.test.StreamingTest.java

public void requete12() {
    System.out.println("Requete 12");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery("SELECT AVG(f.anneeProd) FROM Film f").getSingleResult());
}

From source file:streaming.test.StreamingTest.java

public void requete6() {
    System.out.println("Requete 6");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery(
            "SELECT DISTINCT f FROM Film f Join f.genre g Join f.real r WHERE g.nom = 'Romance' AND r.nom='Coen' ORDER BY f.anneeProd")
            .getResultList());//from   w  w w.j  a v a  2 s  .  com
}

From source file:streaming.test.StreamingTest.java

public void requete8() {
    System.out.println("Requete 8");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery(
            "SELECT count(f) FROM Film f Join f.pays p Join f.genre g WHERE g.nom <> 'Fantastique' AND p.nom='Nouvelle-Zlande'")
            .getResultList());//from ww  w.  jav a  2s  .c o m
}

From source file:streaming.test.StreamingTest.java

public void requete9() {
    System.out.println("Requete 9");
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    System.out.println(em.createQuery(
            "SELECT count(f) FROM Film f Join f.pays p Join f.real r WHERE p.nom='Nouvelle-Zlande' AND r.nom='Jackson'")
            .getResultList());/*w  w  w .  java2  s .c o  m*/
}