javax.persistence.Query.getResultList()方法的使用及代码示例

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

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

Query.getResultList介绍

[英]Execute a SELECT query and return the query results as an untyped List.
[中]执行SELECT查询并将查询结果作为非类型化列表返回。

代码示例

代码示例来源: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: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. @Override
  2. @Transactional(value="defaultTransactionManager")
  3. public void clearDuplicateAccessTokens() {
  4. Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2AccessTokenEntity a GROUP BY a.jwt HAVING count(1) > 1");
  5. @SuppressWarnings("unchecked")
  6. List<Object[]> resultList = query.getResultList();
  7. List<JWT> values = new ArrayList<>();
  8. for (Object[] r : resultList) {
  9. logger.warn("Found duplicate access tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]);
  10. values.add((JWT) r[0]);
  11. }
  12. if (values.size() > 0) {
  13. CriteriaBuilder cb = manager.getCriteriaBuilder();
  14. CriteriaDelete<OAuth2AccessTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2AccessTokenEntity.class);
  15. Root<OAuth2AccessTokenEntity> root = criteriaDelete.from(OAuth2AccessTokenEntity.class);
  16. criteriaDelete.where(root.get("jwt").in(values));
  17. int result = manager.createQuery(criteriaDelete).executeUpdate();
  18. logger.warn("Deleted {} duplicate access tokens", result);
  19. }
  20. }

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

  1. @Test
  2. public void deferredQueryWithResultList() {
  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").getResultList();
  11. verify(query).getResultList();
  12. verify(targetEm).close();
  13. }

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

  1. private <E extends Object> void assertFindByName(EntityManager em, String clazz, E e, String name,
  2. String fieldName)
  3. {
  4. String query = "Select p from PF p where p." + fieldName + " = " + name;
  5. // // find by name.
  6. Query q = em.createQuery(query);
  7. List<E> results = q.getResultList();
  8. Assert.assertNotNull(results);
  9. Assert.assertFalse(results.isEmpty());
  10. Assert.assertEquals(3, results.size());
  11. }

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

  1. private void findByNameAndAgeWithOrClause()
  2. {
  3. EntityManager em;
  4. String query;
  5. Query q;
  6. List<StudentCouchDBDate> students;
  7. int count;
  8. em = emf.createEntityManager();
  9. query = "Select s From StudentOracleNoSQLDate s where s.name = Kuldeep and s.age > " + getMinValue(short.class);
  10. q = em.createQuery(query);
  11. students = q.getResultList();
  12. Assert.assertNotNull(students);
  13. Assert.assertEquals(1, students.size());
  14. count = 0;
  15. for (StudentCouchDBDate student : students)
  16. {
  17. Assert.assertEquals(getMaxValue(Date.class), student.getId());
  18. Assert.assertEquals(getMaxValue(short.class), student.getAge());
  19. Assert.assertEquals("Kuldeep", student.getName());
  20. count++;
  21. }
  22. Assert.assertEquals(1, count);
  23. em.close();
  24. }

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

  1. @SuppressWarnings("unchecked")
  2. public static ArrayList<ProcessInstanceInfo> retrieveProcessInstanceInfo(EntityManagerFactory emf) {
  3. JtaTransactionManager txm = new JtaTransactionManager(null, null, null);
  4. boolean txOwner = txm.begin();
  5. EntityManager em = emf.createEntityManager();
  6. ArrayList<ProcessInstanceInfo> procInstInfoList = new ArrayList<ProcessInstanceInfo>();
  7. List<Object> mdList = em.createQuery("SELECT p FROM ProcessInstanceInfo p").getResultList();
  8. for( Object resultObject : mdList ) {
  9. ProcessInstanceInfo procInstInfo = (ProcessInstanceInfo) resultObject;
  10. procInstInfoList.add(procInstInfo);
  11. logger.trace("> {}", procInstInfo);
  12. }
  13. txm.commit(txOwner);
  14. return procInstInfoList;
  15. }

代码示例来源: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. * @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. @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 max aggregation with nested and or clause.
  3. */
  4. public void testMaxAggregationWithNestedAndOrClause()
  5. {
  6. String nestedQuery = "Select max(p.age) from PersonES p where p.age > 0 AND (p.salary > 250 and (p.personName = 'karthik' OR p.personName = 'pragalbh'))";
  7. Query query = em.createQuery(nestedQuery);
  8. List resultList = query.getResultList();
  9. Assert.assertNotNull(resultList);
  10. Assert.assertEquals(1, resultList.size());
  11. Assert.assertEquals(40.0, resultList.get(0));
  12. }

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

  1. @Override
  2. @Transactional(value="defaultTransactionManager")
  3. public void clearDuplicateRefreshTokens() {
  4. Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2RefreshTokenEntity a GROUP BY a.jwt HAVING count(1) > 1");
  5. @SuppressWarnings("unchecked")
  6. List<Object[]> resultList = query.getResultList();
  7. List<JWT> values = new ArrayList<>();
  8. for (Object[] r : resultList) {
  9. logger.warn("Found duplicate refresh tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]);
  10. values.add((JWT) r[0]);
  11. }
  12. if (values.size() > 0) {
  13. CriteriaBuilder cb = manager.getCriteriaBuilder();
  14. CriteriaDelete<OAuth2RefreshTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2RefreshTokenEntity.class);
  15. Root<OAuth2RefreshTokenEntity> root = criteriaDelete.from(OAuth2RefreshTokenEntity.class);
  16. criteriaDelete.where(root.get("jwt").in(values));
  17. int result = manager.createQuery(criteriaDelete).executeUpdate();
  18. logger.warn("Deleted {} duplicate refresh tokens", result);
  19. }
  20. }

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

  1. private void findByNameAndAgeWithOrClause()
  2. {
  3. EntityManager em;
  4. String query;
  5. Query q;
  6. List<StudentHBaseByteWrapper> students;
  7. int count;
  8. em = emf.createEntityManager();
  9. query = "Select s From StudentHBaseByteWrapper s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
  10. q = em.createQuery(query);
  11. students = q.getResultList();
  12. Assert.assertNotNull(students);
  13. Assert.assertEquals(1, students.size());
  14. count = 0;
  15. for (StudentHBaseByteWrapper student : students)
  16. {
  17. Assert.assertEquals(getMaxValue(Byte.class), student.getId());
  18. Assert.assertEquals(getMaxValue(short.class), student.getAge());
  19. Assert.assertEquals("Kuldeep", student.getName());
  20. count++;
  21. }
  22. Assert.assertEquals(1, count);
  23. em.close();
  24. }

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

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public List<Contact> findRelatives(Contact contact) {
  4. return entityManager.createQuery("SELECT u FROM User u WHERE u.lastname = :lastname") //
  5. .setParameter("lastname", contact.getLastname()) //
  6. .getResultList();
  7. }
  8. }

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

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void testEntityManagerProxyIsProxy() {
  4. EntityManager em = createContainerManagedEntityManager();
  5. assertTrue(Proxy.isProxyClass(em.getClass()));
  6. Query q = em.createQuery("select p from Person as p");
  7. List<Person> people = q.getResultList();
  8. assertTrue(people.isEmpty());
  9. assertTrue("Should be open to start with", em.isOpen());
  10. try {
  11. em.close();
  12. fail("Close should not work on container managed EM");
  13. }
  14. catch (IllegalStateException ex) {
  15. // OK
  16. }
  17. assertTrue(em.isOpen());
  18. }

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

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

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

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void testEntityManagerProxyIsProxy() {
  4. EntityManager em = entityManagerFactory.createEntityManager();
  5. assertTrue(Proxy.isProxyClass(em.getClass()));
  6. Query q = em.createQuery("select p from Person as p");
  7. List<Person> people = q.getResultList();
  8. assertNotNull(people);
  9. assertTrue("Should be open to start with", em.isOpen());
  10. em.close();
  11. assertFalse("Close should work on application managed EM", em.isOpen());
  12. }

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

  1. /**
  2. * Test field with in clause.
  3. */
  4. public void testFieldWithInClause()
  5. {
  6. String queryString = "Select p.personName from PersonES p where p.personId IN ( '1', '2') and p.age = 10";
  7. Query query = em.createQuery(queryString);
  8. List resultList = query.getResultList();
  9. Assert.assertNotNull(resultList);
  10. Assert.assertEquals(1, resultList.size());
  11. Assert.assertEquals("karthik", resultList.get(0));
  12. }

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

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void testMultipleResults() {
  4. // Add with JDBC
  5. String firstName = "Tony";
  6. insertPerson(firstName);
  7. assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
  8. Query q = sharedEntityManager.createQuery("select p from Person as p");
  9. List<Person> people = q.getResultList();
  10. assertEquals(1, people.size());
  11. assertEquals(firstName, people.get(0).getFirstName());
  12. }

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

  1. private void findByNameAndAgeWithOrClause()
  2. {
  3. EntityManager em;
  4. String query;
  5. Query q;
  6. List<StudentHBaseInt> students;
  7. int count;
  8. em = emf.createEntityManager();
  9. query = "Select s From StudentHBaseInt s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
  10. q = em.createQuery(query);
  11. students = q.getResultList();
  12. Assert.assertNotNull(students);
  13. Assert.assertEquals(1, students.size());
  14. count = 0;
  15. for (StudentHBaseInt student : students)
  16. {
  17. Assert.assertEquals(getMaxValue(int.class), student.getId());
  18. Assert.assertEquals(getMaxValue(short.class), student.getAge());
  19. Assert.assertEquals("Kuldeep", student.getName());
  20. count++;
  21. }
  22. Assert.assertEquals(1, count);
  23. em.close();
  24. }

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

  1. @SuppressWarnings({"unchecked"})
  2. private void printPersons(StringBuilder sb) {
  3. List<Person> persons = entityManager.createQuery(
  4. "select p from Person p order by p.id").getResultList();
  5. sb.append("Persons:\n");
  6. for (Person p : persons) {
  7. printPerson(sb, p);
  8. sb.append("\n");
  9. }
  10. }

相关文章