javax.persistence.Query类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(518)

本文整理了Java中javax.persistence.Query类的一些代码示例,展示了Query类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query类的具体详情如下:
包路径:javax.persistence.Query
类名称:Query

Query介绍

[英]Interface used to control query execution.
[中]用于控制查询执行的接口。

代码示例

代码示例来源:origin: kiegroup/jbpm

  1. @SuppressWarnings("unchecked")
  2. public List<String> findContextIdForEvent(String eventType, String ownerId) {
  3. EntityManager em = emf.createEntityManager();
  4. Query findQuery = em.createNamedQuery("FindProcessInstanceWaitingForEvent")
  5. .setParameter("eventType", eventType).setParameter("ownerId", ownerId);
  6. return findQuery.getResultList();
  7. }

代码示例来源:origin: spring-projects/spring-framework

  1. protected void deleteAllPeopleUsingEntityManager(EntityManager em) {
  2. em.createQuery("delete from Person p").executeUpdate();
  3. }

代码示例来源:origin: hibernate/hibernate-orm

  1. private static boolean hasData(EntityManager entityManager) {
  2. return (((Long) entityManager.createQuery( "select count(a) from Address a" ).getSingleResult()) +
  3. ((Long) entityManager.createQuery( "select count(p) from Person p" ).getSingleResult())) > 0;
  4. }
  5. }

代码示例来源:origin: stackoverflow.com

  1. // SQL: SELECT * FROM table LIMIT start, maxRows;
  2. Query q = session.createQuery("FROM table");
  3. q.setFirstResult(start);
  4. q.setMaxResults(maxRows);

代码示例来源:origin: stackoverflow.com

  1. String sql = "SELECT t FROM table t";
  2. Query query = em.createQuery(sql);
  3. query.setFirstResult(firstPosition);
  4. query.setMaxResults(numberOfRecords);
  5. List result = query.getResultList();

代码示例来源:origin: stackoverflow.com

  1. Query empQuery = session.createQuery(hqlQuery);
  2. empQuery.setMaxResults(maxResult);
  3. employees = (List<Employee>) empQuery.list();
  4. for (Employee emp : employees) {
  5. Hibernate.initialize(emp.address);
  6. }

代码示例来源:origin: Impetus/Kundera

  1. /**
  2. * Test positional query.
  3. */
  4. private void testPositionalQuery()
  5. {
  6. String queryString = "Select min(p.salary) from PersonES p where p.personName = ?1";
  7. Query query = em.createQuery(queryString);
  8. query.setParameter(1, "amit");
  9. List resultList = query.getResultList();
  10. Assert.assertEquals(1, resultList.size());
  11. Assert.assertEquals(100.0, resultList.get(0));
  12. }

代码示例来源:origin: spring-projects/spring-data-examples

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public List<Employee> findSubordinates(Employee manager) {
  4. return entityManager.createQuery("SELECT u from User u where u.manager = :manager") //
  5. .setParameter("manager", manager) //
  6. .getResultList();
  7. }
  8. }

代码示例来源:origin: spring-projects/spring-integration-samples

  1. @SuppressWarnings("unchecked")
  2. public static List<LoanShark> findLoanSharkEntries(int firstResult, int maxResults) {
  3. return entityManager().createQuery("select o from LoanShark o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
  4. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void testEntityManagerProxyIsProxy() {
  4. assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
  5. Query q = sharedEntityManager.createQuery("select p from Person as p");
  6. q.getResultList();
  7. assertTrue("Should be open to start with", sharedEntityManager.isOpen());
  8. sharedEntityManager.close();
  9. assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
  10. }

代码示例来源:origin: Impetus/Kundera

  1. /**
  2. * Test max aggregation.
  3. */
  4. private void testMaxAggregation()
  5. {
  6. String queryString = "Select max(p.age) from PersonES p";
  7. Query query = em.createQuery(queryString);
  8. List resultList = query.getResultList();
  9. Assert.assertEquals(1, resultList.size());
  10. Assert.assertEquals(40.0, resultList.get(0));
  11. }

代码示例来源:origin: hibernate/hibernate-orm

  1. final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "hibernate-osgi-test", null );
  2. EntityManager em = emf.createEntityManager();
  3. em.getTransaction().begin();
  4. em.persist( new DataPoint( "Brett" ) );
  5. em.getTransaction().commit();
  6. em.close();
  7. em = emf.createEntityManager();
  8. em.getTransaction().begin();
  9. DataPoint dp = em.find( DataPoint.class, 1 );
  10. em.close();
  11. em = emf.createEntityManager();
  12. em.getTransaction().begin();
  13. dp = em.find( DataPoint.class, 1 );
  14. em = emf.createEntityManager();
  15. em.getTransaction().begin();
  16. em.createQuery( "delete from DataPoint" ).executeUpdate();
  17. em.getTransaction().commit();
  18. em.close();

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public List<FulfillmentGroup> readUnprocessedFulfillmentGroups(int start,
  4. int maxResults) {
  5. Query query = em.createNamedQuery("BC_READ_UNPROCESSED_FULFILLMENT_GROUP_ASC");
  6. query.setFirstResult(start);
  7. query.setMaxResults(maxResults);
  8. return query.getResultList();
  9. }

代码示例来源:origin: kiegroup/jbpm

  1. @Override
  2. protected int getProcessInstancesCount() {
  3. boolean txOwner = false;
  4. if( useTransactions ) {
  5. txOwner = txm.begin();
  6. }
  7. int size = emf.createEntityManager().createQuery( "FROM ProcessInstanceInfo" ).getResultList().size();
  8. if( useTransactions ) {
  9. txm.commit(txOwner);
  10. }
  11. return size;
  12. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void testQueryNoPersons() {
  4. EntityManager em = entityManagerFactory.createEntityManager();
  5. Query q = em.createQuery("select p from Person as p");
  6. List<Person> people = q.getResultList();
  7. assertEquals(0, people.size());
  8. try {
  9. assertNull(q.getSingleResult());
  10. fail("Should have thrown NoResultException");
  11. }
  12. catch (NoResultException ex) {
  13. // expected
  14. }
  15. }

代码示例来源:origin: Impetus/Kundera

  1. /**
  2. * Test delete all.
  3. */
  4. private void testDeleteAll()
  5. {
  6. persistBooks();
  7. int result = em.createQuery("delete from Book b").executeUpdate();
  8. Assert.assertEquals(4, result);
  9. assertDeleted(T, T, T, T);
  10. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void test() {
  3. doInJPA( this::sessionFactory, entityManager -> {
  4. Thing thing = new Thing();
  5. entityManager.persist( thing );
  6. entityManager
  7. .createQuery( "update thing set special = :s, version = version + 1" )
  8. .setParameter( "s", "new" )
  9. .executeUpdate();
  10. thing.special = "If I'm flush to the DB you get an OptimisticLockException";
  11. } );
  12. }

代码示例来源:origin: Impetus/Kundera

  1. /**
  2. * @param b
  3. */
  4. private void testNativeQuery(boolean b)
  5. {
  6. String s = "Select * From " + "\"StudentUUID\"";
  7. EntityManager em = emf.createEntityManager();
  8. Query q = em.createNativeQuery(s, StudentUUID.class);
  9. List<StudentUUID> results = q.getResultList();
  10. Assert.assertNotNull(results);
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void deferredQueryWithSingleResult() {
  3. EntityManagerFactory emf = mock(EntityManagerFactory.class);
  4. EntityManager targetEm = mock(EntityManager.class);
  5. Query query = mock(Query.class);
  6. given(emf.createEntityManager()).willReturn(targetEm);
  7. given(targetEm.createQuery("x")).willReturn(query);
  8. given(targetEm.isOpen()).willReturn(true);
  9. EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  10. em.createQuery("x").getSingleResult();
  11. verify(query).getSingleResult();
  12. verify(targetEm).close();
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void deferredQueryWithUpdate() {
  3. EntityManagerFactory emf = mock(EntityManagerFactory.class);
  4. EntityManager targetEm = mock(EntityManager.class);
  5. Query query = mock(Query.class);
  6. given(emf.createEntityManager()).willReturn(targetEm);
  7. given(targetEm.createQuery("x")).willReturn(query);
  8. given(targetEm.isOpen()).willReturn(true);
  9. EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  10. em.createQuery("x").executeUpdate();
  11. verify(query).executeUpdate();
  12. verify(targetEm).close();
  13. }

相关文章