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:streaming.StreamingTest.java

public void filmFantastiqueOrdreAlphaSansJoin() {
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    List<Film> filmsFantastiques = em
            .createQuery("SELECT f FROM Film f WHERE f.genre.nom = 'Fantastique' ORDER BY f.titre ASC")
            .getResultList();// w w w .  j  ava  2s  . c  o  m
    for (Film f : filmsFantastiques) {
        System.out.println(f.getTitre());
    }

}

From source file:BO.UserHandler.java

public boolean respondFriendRequest(VUser user) {
    EntityManager em;

    EntityManagerFactory emf;//from  ww w . j  a  v  a  2s.  co  m
    emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    em = emf.createEntityManager();

    try {
        em.getTransaction().begin();
        System.out.println("Receiving friend: " + user.getEmail());
        System.out.println("Sending friend: " + user.getFriendToAdd());

        Friendship f = (Friendship) em
                .createQuery("SELECT f from Friendship f WHERE f.receivingFriend.email LIKE :email1 "
                        + "AND f.sendingFriend.email LIKE :email2")
                .setParameter("email1", user.getEmail()).setParameter("email2", user.getFriendToAdd())
                .getSingleResult();
        f.setDidRespond(true);
        f.setDidAccept(user.isDidAccept());
        em.persist(f);
        em.flush();
        em.getTransaction().commit();
        return true;
    } catch (Exception e) {
        System.out.println(e);
        return false;
    } finally {
        if (em != null) {
            em.close();
        }
        emf.close();
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

/**
 * selects a newsdrilldown by the FirstSpirit-ID.
 *
 * @param fs_id    the FirstSpirit ID//from  ww  w.j  av a  2 s. c  o  m
 * @param language the language
 * @return the newsdrilldown or null
 * @throws Exception the exception
 */
private News getNews(Long fs_id, String language) throws Exception {

    EntityManager em = null;
    try {
        em = emf.createEntityManager();

        Query query = em.createQuery(new StringBuilder()
                .append("SELECT x FROM news x WHERE x.fs_id = :fs_id AND x.language = :language").toString());
        query.setParameter("fs_id", fs_id);
        query.setParameter("language", language);

        if (query.getResultList().size() > 0) {
            return (News) query.getSingleResult();
        }

        return null;
    } finally {

        if (em != null) {
            em.close();
        }
    }
}

From source file:streaming.StreamingTest.java

public void filmsNeoZelandaisFantastiquesPeterJackson() {
    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    Long filmsFantastiquePeter = (Long) em.createQuery(
            "SELECT COUNT (f) FROM Film f JOIN f.genre g JOIN f.pays p JOIN f.listeRealisateurs r WHERE g.nom = 'Fantastique' AND p.nom = 'Nouvelle-Zlande' AND r.prenom = 'Peter'")
            .getSingleResult();//from  w  w w.j ava2 s. co  m
    System.out.println(filmsFantastiquePeter);

}

From source file:BO.UserHandler.java

public boolean addFriend(VUser u) {
    EntityManager em;
    EntityManagerFactory emf;//from   w  w w . java2  s. co m
    emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    em = emf.createEntityManager();

    try {
        em.getTransaction().begin();
        System.out.println(u.getEmail());
        System.out.println(u.getFriendToAdd());

        try {
            List<Friendship> f = (List<Friendship>) em
                    .createQuery("SELECT f from Friendship f WHERE f.receivingFriend.email LIKE :email1 "
                            + "AND f.sendingFriend.email LIKE :email2")
                    .setParameter("email1", u.getEmail()).setParameter("email2", u.getFriendToAdd())
                    .getResultList();
            if (!f.isEmpty()) {
                return false;
            }
        } catch (Exception e) {

        }

        try {

            List<Friendship> f = (List<Friendship>) em
                    .createQuery("SELECT f from Friendship f WHERE f.sendingFriend.email LIKE :email1 "
                            + "AND f.receivingFriend.email LIKE :email2")
                    .setParameter("email1", u.getEmail()).setParameter("email2", u.getFriendToAdd())
                    .getResultList();
            if (!f.isEmpty()) {
                return false;
            }
        } catch (Exception e) {

        }

        User sender = getUserByEmail(u.getEmail());
        User receiver = getUserByEmail(u.getFriendToAdd());

        Friendship friendship = new Friendship(sender, receiver);
        em.persist(friendship);
        em.flush();
        em.getTransaction().commit();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (em != null) {
            em.close();
        }
        emf.close();
    }
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

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

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*  w ww.ja va 2  s . c  om*/

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

    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

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

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from w w w .  j  a v  a  2s.  c o  m

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

    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

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

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/* w  w w .  j  a  v a  2  s  .  c om*/

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

    entityTransaction.commit();

}

From source file:streaming.StreamingTest.java

public void filmsParRealisateur() {

    EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    Collection<Object[]> nbFilmParRealisateur = em.createQuery(
            "SELECT COUNT (f) AS total, r FROM Realisateur r JOIN r.listeFilmsParRealisateur f GROUP BY r ORDER BY total")
            .getResultList();/*from  w  ww  .  j  a v  a2 s .com*/

    for (Object[] r : nbFilmParRealisateur) {

        long total = (long) r[0];
        Realisateur real = (Realisateur) r[1];

        System.out.println(real.getNom() + " " + real.getPrenom() + " " + total);
    }
}

From source file:edu.sabanciuniv.sentilab.sare.models.base.documentStore.PersistentDocumentStore.java

/**
 * Gets identifiers of documents in this store.
 * @param em the {@link EntityManager} to get the documents from.
 * @return an {@link Iterable} of document identifiers.
 *//*from   w  w w  .  jav  a  2 s.  c o  m*/
public Iterable<byte[]> getDocumentIds(EntityManager em) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class);
    Root<PersistentDocument> doc = cq.from(PersistentDocument.class);
    cq.multiselect(doc.get("id"))
            .where(cb.equal(doc.get("store"), cb.parameter(PersistentDocumentStore.class, "store")));
    TypedQuery<byte[]> tq = em.createQuery(cq);
    tq.setParameter("store", this);
    return tq.getResultList();
}