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:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public PersonaFirma findPersonaFirma(Long idPersona, String firma) {
    EntityManager em = null;
    try {//from w ww. j  a  va 2  s .  com
        em = getEntityManager();
        Query q = em.createQuery(
                "select distinct pf from PersonaFirma pf where pf.persona.id=:idPersona and pf.firma.nombre=:firma");
        q.setParameter("idPersona", idPersona);
        q.setParameter("firma", firma);
        List<PersonaFirma> list = q.getResultList();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public Persona findPersona(Long id) {
    EntityManager em = null;
    try {/*www  .  j  a  v  a2 s  . com*/
        em = getEntityManager();
        Query q = em.createQuery(
                "select p from Persona p left join fetch p.personaTitulosCollection where p.id=:id");
        q.setParameter("id", id);
        List<Persona> list = q.getResultList();
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.test.CommandITCase.java

/**
 * Test delete.//from   www .  j a v  a2  s.  com
 *
 * @throws Exception the exception
 */
@Test
public void testDelete() throws Exception {

    assertEquals("DB not empty", 0, countArticles());

    EntityManager em = emf.createEntityManager();

    String[] ids = new String[] { "128", "130", "131", "132", "256", "704" };

    // insert all items
    for (String id : ids) {
        // item should not be in the db
        Query query = em.createQuery(
                new StringBuilder().append("SELECT x FROM article x WHERE x.aid = ").append(id).toString());
        assertEquals(0, query.getResultList().size());

        // load content
        String content = getContent("src/test/resources/inbox/add/pressreleasesdetails_" + id + ".xml",
                "hibernate");
        // send content to jms broker
        template.sendBody("jms:topic:BUS_OUT", content);
    }
    // wait
    Thread.sleep(TimeOuts.LONG);

    assertEquals("not all items are present", ids.length, countArticles());

    // now check, that items are not insert twice if resend the same content
    for (String id : ids) {
        // load content
        String content = getContent("src/test/resources/inbox/delete/pressreleasesdetails_" + id + ".xml",
                "hibernate");
        // send content to jms broker
        template.sendBody("jms:topic:BUS_OUT", content);
        // wait
        Thread.sleep(TimeOuts.LONG);

        // item should be deleted
        Query query = em.createQuery(
                new StringBuilder().append("SELECT x FROM article x WHERE x.aid = ").append(id).toString());
        assertEquals(0, query.getResultList().size());
    }

    assertEquals("ups, there are items left in the db", 0, countArticles());

    em.close();
}

From source file:corner.orm.gae.impl.PaginatedJapEntityService.java

License:asdf

public Iterator find(final Class<?> persistClass, final Object conditions, final String order, final int start,
        final int offset) {
    return (Iterator) this.template.execute(new JpaCallback() {
        @Override//from  w w w.ja  va  2s. com
        public Object doInJpa(EntityManager entityManager) throws PersistenceException {
            Iterable con = typeCoercer.coerce(conditions, Iterable.class);
            final Iterator it = con == null ? null : con.iterator();
            final StringBuffer sb = buildConditionJPQL(persistClass, it);
            appendOrder(sb, order);
            sb.insert(0, "select root." + EntityConstants.ID_PROPERTY_NAME);
            Query query = entityManager.createQuery(sb.toString());
            if (it != null) {
                int i = 0;
                while (it.hasNext()) {
                    query.setParameter(String.valueOf(++i), it.next());
                }
            }
            query.setFirstResult(start);
            query.setMaxResults(offset);
            return query.getResultList().iterator();
        }
    });
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.test.CommandITCase.java

/**
 * Test add.//from   www. java  2  s . c o m
 *
 * @throws Exception the exception
 */
@Test
public void testAdd() throws Exception {

    assertEquals("DB not empty", 0, countArticles());

    EntityManager em = emf.createEntityManager();

    String[] ids = new String[] { "128", "130", "131", "132", "256", "704" };

    // insert all items
    for (String id : ids) {
        // item should not be in the db
        Query query = em.createQuery(
                new StringBuilder().append("SELECT x FROM article x WHERE x.aid = ").append(id).toString());
        assertEquals(0, query.getResultList().size());

        // load content
        String content = getContent("src/test/resources/inbox/add/pressreleasesdetails_" + id + ".xml",
                "hibernate");
        // send content to jms broker
        template.sendBody("jms:topic:BUS_OUT", content);

        // wait
        Thread.sleep(TimeOuts.LONG);

        // item should be inserted to db
        query = em.createQuery(
                new StringBuilder().append("SELECT x FROM article x WHERE x.aid = ").append(id).toString());
        assertEquals(1, query.getResultList().size());
    }

    assertEquals("not all items are present", ids.length, countArticles());

    // now check, that items are not insert twice if resend the same content
    for (String id : ids) {
        // load content
        String content = getContent("src/test/resources/inbox/add/pressreleasesdetails_" + id + ".xml",
                "hibernate");
        // send content to jms broker
        template.sendBody("jms:topic:BUS_OUT", content);
        // wait
        Thread.sleep(TimeOuts.LONG);

        // only one item should be present
        Query query = em.createQuery(
                new StringBuilder().append("SELECT x FROM article x WHERE x.aid = ").append(id).toString());
        assertEquals(1, query.getResultList().size());
    }

    em.close();
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Before test./*from  ww  w  . ja v a2  s. c  o  m*/
 *
 * @throws Exception the exception
 */
@Before
public void beforeTest() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        EntityTransaction et = em.getTransaction();
        et.begin();
        Query query = em.createQuery(new StringBuilder().append("SELECT x FROM news x").toString());
        List<News> newsList = query.getResultList();
        for (News news : newsList) {
            em.remove(news);
        }

        query = em.createQuery(new StringBuilder().append("SELECT x FROM category x").toString());
        List<NewsCategory> catList = query.getResultList();
        for (NewsCategory temp : catList) {
            em.remove(temp);
        }

        query = em.createQuery(new StringBuilder().append("SELECT x FROM metaCategory x").toString());
        List<NewsMetaCategory> metaList = query.getResultList();
        for (NewsMetaCategory temp : metaList) {
            em.remove(temp);
        }
        et.commit();
    } finally {
        em.close();
    }

}

From source file:com.enioka.jqm.tools.JqmEngine.java

@Override
public void pause() {
    EntityManager em = Helpers.getNewEm();
    em.getTransaction().begin();/* w  w  w  . j a  va  2s  .  c  o m*/
    em.createQuery("UPDATE Node n SET n.enabled = false WHERE n.id = :id").setParameter("id", node.getId())
            .executeUpdate();
    em.getTransaction().commit();
    em.close();
    refreshConfiguration();
}

From source file:com.enioka.jqm.tools.JqmEngine.java

@Override
public void resume() {
    EntityManager em = Helpers.getNewEm();
    em.getTransaction().begin();/*from  w  w  w.jav  a 2  s.  c o m*/
    em.createQuery("UPDATE Node n SET n.enabled = true WHERE n.id = :id").setParameter("id", node.getId())
            .executeUpdate();
    em.getTransaction().commit();
    em.close();
    refreshConfiguration();
}

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

public FStoreUser readFStoreUserByUsername(String username) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Query q = entityManager.createQuery("SELECT m FROM FStoreUser m WHERE m.username='" + username + "'");
    return (q.getResultList().size() == 0) ? null : (FStoreUser) q.getSingleResult();
}

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

public FStoreUser readFStoreUserByEmail(String email) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Query q = entityManager.createQuery("SELECT m FROM FStoreUser m WHERE m.email='" + email + "'");
    return (q.getResultList().size() == 0) ? null : (FStoreUser) q.getSingleResult();
}