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

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

本文整理了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

@Override
  @SuppressWarnings("unchecked")
  public List<Employee> findSubordinates(Employee manager) {

    return entityManager.createQuery("SELECT u from User u where u.manager = :manager") //
        .setParameter("manager", manager) //
        .getResultList();
  }
}

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

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

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

@Test
public void deferredQueryWithResultList() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").getResultList();
  verify(query).getResultList();
  verify(targetEm).close();
}

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

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

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

private void findByNameAndAgeWithOrClause()
{
  EntityManager em;
  String query;
  Query q;
  List<StudentCouchDBDate> students;
  int count;
  em = emf.createEntityManager();
  query = "Select s From StudentOracleNoSQLDate s where s.name = Kuldeep and s.age > " + getMinValue(short.class);
  q = em.createQuery(query);
  students = q.getResultList();
  Assert.assertNotNull(students);
  Assert.assertEquals(1, students.size());
  count = 0;
  for (StudentCouchDBDate student : students)
  {
    Assert.assertEquals(getMaxValue(Date.class), student.getId());
    Assert.assertEquals(getMaxValue(short.class), student.getAge());
    Assert.assertEquals("Kuldeep", student.getName());
    count++;
  }
  Assert.assertEquals(1, count);
  em.close();
}

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

@SuppressWarnings("unchecked")
public static ArrayList<ProcessInstanceInfo> retrieveProcessInstanceInfo(EntityManagerFactory emf) { 
  
  JtaTransactionManager txm = new JtaTransactionManager(null, null, null);
  boolean txOwner = txm.begin();

  EntityManager em = emf.createEntityManager();
  
  ArrayList<ProcessInstanceInfo> procInstInfoList = new ArrayList<ProcessInstanceInfo>();
  List<Object> mdList = em.createQuery("SELECT p FROM ProcessInstanceInfo p").getResultList();
  for( Object resultObject : mdList ) { 
    ProcessInstanceInfo procInstInfo = (ProcessInstanceInfo) resultObject;
    procInstInfoList.add(procInstInfo);
    logger.trace("> {}", procInstInfo);
  }
  
  txm.commit(txOwner);
  
  return procInstInfoList;
}

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

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

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

/**
 * @param b
 */
private void testNativeQuery(boolean b)
{
  String s = "Select * From " + "\"StudentUUID\"";
  EntityManager em = emf.createEntityManager();
  Query q = em.createNativeQuery(s, StudentUUID.class);
  List<StudentUUID> results = q.getResultList();
  Assert.assertNotNull(results);
}

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

@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
  EntityManager em = entityManagerFactory.createEntityManager();
  Query q = em.createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertEquals(0, people.size());
  try {
    assertNull(q.getSingleResult());
    fail("Should have thrown NoResultException");
  }
  catch (NoResultException ex) {
    // expected
  }
}

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

/**
 * Test max aggregation with nested and or clause.
 */
public void testMaxAggregationWithNestedAndOrClause()
{
  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'))";
  Query query = em.createQuery(nestedQuery);
  List resultList = query.getResultList();
  Assert.assertNotNull(resultList);
  Assert.assertEquals(1, resultList.size());
  Assert.assertEquals(40.0, resultList.get(0));
}

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

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

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

private void findByNameAndAgeWithOrClause()
{
  EntityManager em;
  String query;
  Query q;
  List<StudentHBaseByteWrapper> students;
  int count;
  em = emf.createEntityManager();
  query = "Select s From StudentHBaseByteWrapper s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
  q = em.createQuery(query);
  students = q.getResultList();
  Assert.assertNotNull(students);
  Assert.assertEquals(1, students.size());
  count = 0;
  for (StudentHBaseByteWrapper student : students)
  {
    Assert.assertEquals(getMaxValue(Byte.class), student.getId());
    Assert.assertEquals(getMaxValue(short.class), student.getAge());
    Assert.assertEquals("Kuldeep", student.getName());
    count++;
  }
  Assert.assertEquals(1, count);
  em.close();
}

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

@Override
  @SuppressWarnings("unchecked")
  public List<Contact> findRelatives(Contact contact) {

    return entityManager.createQuery("SELECT u FROM User u WHERE u.lastname = :lastname") //
        .setParameter("lastname", contact.getLastname()) //
        .getResultList();
  }
}

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

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
  EntityManager em = createContainerManagedEntityManager();
  assertTrue(Proxy.isProxyClass(em.getClass()));
  Query q = em.createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertTrue(people.isEmpty());
  assertTrue("Should be open to start with", em.isOpen());
  try {
    em.close();
    fail("Close should not work on container managed EM");
  }
  catch (IllegalStateException ex) {
    // OK
  }
  assertTrue(em.isOpen());
}

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

/**
 * @param b
 */
private void testNativeQuery(boolean b)
{
  String s = "Select * From " + "\"StudentOracleNoSQLUUID\"";
  EntityManager em = emf.createEntityManager();
  Query q = em.createNativeQuery(s, StudentCouchDBUUID.class);
  List<StudentCouchDBUUID> results = q.getResultList();
  Assert.assertNotNull(results);
}

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

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

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

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

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

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

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

private void findByNameAndAgeWithOrClause()
{
  EntityManager em;
  String query;
  Query q;
  List<StudentHBaseInt> students;
  int count;
  em = emf.createEntityManager();
  query = "Select s From StudentHBaseInt s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
  q = em.createQuery(query);
  students = q.getResultList();
  Assert.assertNotNull(students);
  Assert.assertEquals(1, students.size());
  count = 0;
  for (StudentHBaseInt student : students)
  {
    Assert.assertEquals(getMaxValue(int.class), student.getId());
    Assert.assertEquals(getMaxValue(short.class), student.getAge());
    Assert.assertEquals("Kuldeep", student.getName());
    count++;
  }
  Assert.assertEquals(1, count);
  em.close();
}

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

@SuppressWarnings({"unchecked"})
private void printPersons(StringBuilder sb) {
  List<Person> persons = entityManager.createQuery(
      "select p from Person p order by p.id").getResultList();
  sb.append("Persons:\n");
  for (Person p : persons) {
    printPerson(sb, p);
    sb.append("\n");
  }
}

相关文章