List of usage examples for javax.persistence EntityManager createQuery
public Query createQuery(CriteriaDelete deleteQuery);
Query
for executing a criteria delete query. From source file:com.appdynamicspilot.persistence.CartPersistence.java
@Transactional public void deleteAllCartItems(Long userId) { EntityManager em = getEntityManager(); EntityTransaction txn = em.getTransaction(); txn.begin();/*from w w w . j a va 2s . co m*/ Query q = em.createQuery("DELETE FROM Cart c where c.user.id=:id"); q.setParameter("id", userId); q.executeUpdate(); txn.commit(); }
From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java
public List<Task> read() throws SQLException { EntityManager manager = getEntityManager(); CriteriaQuery criteria = manager.getCriteriaBuilder().createQuery(); criteria.select(criteria.from(Task.class)); return manager.createQuery(criteria).getResultList(); }
From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.NewsITCase.java
/** * Before test./*w ww . j a va 2 s . c o m*/ * * @throws Exception the exception */ @Before public void beforeTest() throws Exception { EntityManager em = emf.createEntityManager(); try { 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); } } finally { em.close(); } }
From source file:de.iai.ilcd.model.dao.UserDao.java
public User getUser(String userName) { EntityManager em = PersistenceUtil.getEntityManager(); User user = null;/* w w w. jav a 2 s. c o m*/ try { user = (User) em.createQuery("select user from User user where user.userName=:userName") .setParameter("userName", userName).getSingleResult(); } catch (NoResultException e) { // we do nothing here; just return null } return user; }
From source file:com.ewcms.common.query.jpa.QueryInit.java
private void clean() { getJpaTemplate().execute(new JpaCallback<Object>() { @Override//w ww . j ava 2 s . co m public Object doInJpa(EntityManager em) throws PersistenceException { String hql = "Delete From LimitLog"; em.createQuery(hql).executeUpdate(); hql = "Delete From Certificate"; em.createQuery(hql).executeUpdate(); hql = "Delete From Sex"; em.createQuery(hql).executeUpdate(); return null; } }); }
From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java
/** * /*w w w . j a v a2 s . c om*/ * @return */ public long getInprogressTasksCount() { EntityManager em = getEntityManager(); try { return (long) em.createQuery("SELECT COUNT(st.id) FROM StatusTrail st WHERE " + " st.status = 'in_progress' AND st.givenUp = 0").getSingleResult(); } finally { em.close(); } }
From source file:com.sun.socialsite.business.impl.JPAPersistenceStrategy.java
/** * Create query from queryString with FlushModeType.COMMIT * @param queryString the quuery// w w w . j av a2s . co m * @throws SocialSiteException on any error */ public Query getDynamicQuery(String queryString) throws SocialSiteException { EntityManager em = getEntityManager(false); Query q = em.createQuery(queryString); // Never flush for queries. SocialSite code assumes this behavior q.setFlushMode(FlushModeType.COMMIT); return q; }
From source file:corner.orm.gae.impl.PaginatedJpaEntityServiceTest.java
@Test public void test_paginate() { EntityManager entityManager = newMock(EntityManager.class); Query query = newMock(Query.class); Query query2 = newMock(Query.class); expect(entityManager .createQuery("select root.id from corner.orm.gae.impl.TestAEntity as root where name=:1")) .andReturn(query);// w w w . jav a 2s .c o m expect(entityManager .createQuery("select count(root) from corner.orm.gae.impl.TestAEntity as root where name=:1")) .andReturn(query2); expect(query.setParameter("1", "acai")).andReturn(query); expect(query2.setParameter("1", "acai")).andReturn(query2); expect(query.setFirstResult(0)).andReturn(query); expect(query.setMaxResults(10)).andReturn(query); List listValue = Collections.EMPTY_LIST; expect(query.getResultList()).andReturn(listValue); expect(query2.getSingleResult()).andReturn(new Integer(1234)); JpaTemplate jpaTemplate = GaeModule.buildJpaTemplate(entityManager); replay(); PaginatedJapEntityService pjes = new PaginatedJapEntityService(jpaTemplate, typeCoercer); PaginationOptions options = new PaginationOptions(); PaginationList pl = pjes.paginate(TestAEntity.class, new String[] { "name=:1", "acai" }, null, options); assertFalse(((Iterator) pl.collectionObject()).hasNext()); PaginationOptions optionsR = pl.options(); assertEquals(1234, optionsR.getTotalRecord()); verify(); }
From source file:corner.orm.gae.impl.PaginatedJpaEntityServiceTest.java
@Test public void test_paginate_with_order() { EntityManager entityManager = newMock(EntityManager.class); Query query = newMock(Query.class); Query query2 = newMock(Query.class); expect(entityManager.createQuery( "select root.id from corner.orm.gae.impl.TestAEntity as root where name=:1 order by id desc")) .andReturn(query);/*from w ww . ja va2 s. c o m*/ expect(entityManager .createQuery("select count(root) from corner.orm.gae.impl.TestAEntity as root where name=:1")) .andReturn(query2); expect(query.setParameter("1", "acai")).andReturn(query); expect(query2.setParameter("1", "acai")).andReturn(query2); expect(query.setFirstResult(0)).andReturn(query); expect(query.setMaxResults(10)).andReturn(query); List listValue = Collections.EMPTY_LIST; expect(query.getResultList()).andReturn(listValue); expect(query2.getSingleResult()).andReturn(new Integer(1234)); JpaTemplate jpaTemplate = GaeModule.buildJpaTemplate(entityManager); replay(); PaginatedJapEntityService pjes = new PaginatedJapEntityService(jpaTemplate, typeCoercer); PaginationOptions options = new PaginationOptions(); PaginationList pl = pjes.paginate(TestAEntity.class, new String[] { "name=:1", "acai" }, "id desc", options); assertFalse(((Iterator) pl.collectionObject()).hasNext()); PaginationOptions optionsR = pl.options(); assertEquals(1234, optionsR.getTotalRecord()); verify(); }
From source file:com.sdl.odata.datasource.jpa.JPADatasourceProvider.java
private <T> List<T> executeQueryListResult(JPAQuery jpaQuery) { EntityManager em = entityManagerFactory.createEntityManager(); String queryString = jpaQuery.getQueryString(); Query query = em.createQuery(queryString); int nrOfResults = jpaQuery.getLimitCount(); int startPosition = jpaQuery.getSkipCount(); Map<String, Object> queryParams = jpaQuery.getQueryParams(); try {/*from w w w . j a va 2s.c om*/ if (nrOfResults > 0) { query.setMaxResults(nrOfResults); } if (startPosition > 0) { query.setFirstResult(startPosition); } for (Map.Entry<String, Object> entry : queryParams.entrySet()) { query.setParameter(entry.getKey(), tryConvert(entry.getValue())); } return query.getResultList(); } finally { em.close(); } }