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:de.iai.ilcd.model.dao.UserDao.java

@SuppressWarnings("unchecked")
public List<User> getUsers() {
    EntityManager em = PersistenceUtil.getEntityManager();
    List<User> users = em.createQuery("select user from User user order by user.userName").getResultList();
    return users;
}

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

@Test
public void test_count() {
    EntityManager entityManager = newMock(EntityManager.class);
    Query query = newMock(Query.class);
    expect(entityManager
            .createQuery("select count(root)  from corner.orm.gae.impl.TestAEntity as root  where name=?"))
                    .andReturn(query);//ww  w  .  j  a  va2s  .  c  om
    expect(query.setParameter(0, "acai")).andReturn(query);
    expect(query.getSingleResult()).andReturn(new Integer(1234));
    JpaTemplate jpaTemplate = GaeModule.buildJpaTemplate(entityManager);
    replay();
    PaginatedJapEntityService pjes = new PaginatedJapEntityService(jpaTemplate, typeCoercer);
    assertEquals(1234, pjes.count(TestAEntity.class, new String[] { "name=?", "acai" }));
    verify();
}

From source file:com.soen.smbank.dao.ObjectDao.java

public ArrayList getAllObjectsByCondition(String tableName, String whereString) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;//from  ww  w.  j  a v a  2s.  c om
    try {
        entities = (ArrayList) em.createQuery("SELECT tb FROM " + tableName + " tb WHERE " + whereString)
                .getResultList();

        return entities;
    } finally {
        em.close();
    }
}

From source file:com.taobao.ad.easyschedule.dao.reportjobrt.impl.JPAReportJobRtDAOImpl.java

@SuppressWarnings("unchecked")
@Override// w  w w . j a  va 2 s  .com
public List<ReportJobRtDO> findReportJobRt(final ReportJobRtDO reportJobRt) {

    return (List<ReportJobRtDO>) super.getJpaTemplate().execute(new JpaCallback() {

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

            return em
                    .createQuery(
                            "select t from es_report_job_rt t where  t.createTime<=?1 and t.createTime >=?2")
                    .setParameter(1, reportJobRt.getQueryEndTime())
                    .setParameter(2, reportJobRt.getQueryStartTime()).getResultList();

        }
    });

}

From source file:de.iai.ilcd.model.dao.UserGroupDao.java

@SuppressWarnings("unchecked")
public List<UserGroup> getOrgLessGroups() {
    EntityManager em = PersistenceUtil.getEntityManager();

    try {/*from   w ww . j  a  v  a  2  s .c o  m*/
        return (List<UserGroup>) em
                .createQuery(
                        "SELECT DISTINCT g FROM UserGroup g WHERE g.organization is NULL ORDER BY g.groupName")
                .getResultList();
    } catch (Exception e) {
        return null;
    }
}

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

/**
 * Count news.//from   w  w  w  .j av a  2s. c o m
 *
 * @return the long
 * @throws Exception the exception
 */
private long countNews() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.fs_id) FROM news p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}

From source file:de.iai.ilcd.model.dao.UserGroupDao.java

@SuppressWarnings("unchecked")
public List<UserGroup> getGroups(Organization org) {
    if (org == null) {
        return null;
    }//from  w w  w  .j  a  v a  2s .c  o  m
    EntityManager em = PersistenceUtil.getEntityManager();

    try {
        return (List<UserGroup>) em.createQuery(
                "SELECT DISTINCT g FROM UserGroup g WHERE g.organization.id = :orgId ORDER BY g.groupName")
                .setParameter("orgId", org.getId()).getResultList();
    } catch (Exception e) {
        return null;
    }
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketCategoryDAOJpa.java

private long getNodeCount() {
    return (Long) this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery("SELECT COUNT(tc) FROM TicketCategory tc");
            return q.getSingleResult();
        }//from  w  w w.j  av a  2  s .  com
    });
}

From source file:net.anthonychaves.bookmarks.service.TagService.java

public List<BookmarkDetail> findBookmarksByTag(String tag, User user) {
    List<byte[]> bookmarkIds = new ArrayList<byte[]>();
    try {// w  w w  .  j a  v  a  2  s . com
        bookmarkIds = jredis.lrange(tag, 0, 50000);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    User u = em.find(User.class, user.getId());
    javax.persistence.Query query = em.createQuery(
            "select new net.anthonychaves.bookmarks.models.BookmarkDetail(b.id, b.title, b.url, b.tags) from Bookmark b where b.id in (:ids) and b.user = :user order by b.id desc")
            .setParameter("ids", bookmarkIds).setParameter("user", u);
    List<BookmarkDetail> bookmarks = (List<BookmarkDetail>) query.getResultList();
    em.getTransaction().rollback();

    return bookmarks;
}

From source file:com.soen.ebanking.dao.ObjectDao.java

public ArrayList getAllObjectsByCondition(String tableName, String whereString) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;//  w ww .  j  av  a  2  s . com
    try {
        entities = (ArrayList) em.createQuery("SELECT t FROM " + tableName + " t WHERE " + whereString)
                .getResultList();

        return entities;
    } finally {
        em.close();
    }
}