Close

JPQL - Returning Tuple results

[Updated: Aug 4, 2018, Created: Aug 4, 2018]

Following example shows how to retrieve query results of type Tuple. Tuple contains ordered query results which can be accessed in different ways. Following is Tuple snippet:

package javax.persistence;
 ...
public interface Tuple {
     <X> X get(TupleElement<X> tupleElement);
     <X> X get(String alias, Class<X> type);
     Object get(String alias);
     <X> X get(int i, Class<X> type);
     Object get(int i);
     Object[] toArray();
     List<TupleElement<?>> getElements();
}

We can use following method of EntityManager to create Tuple query:

public <T> TypedQuery<T> createQuery(String query, Class<T> resultClass);

For example:

  TypedQuery<Tuple> query = entityManager.createQuery("SELECT e.name, e.salary FROM Employee e", Tuple.class);
  List<Tuple> resultList = query.getResultList();

Example

Entity

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private long salary;
  private String dept;
    .............
}

Using Tuple

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployeeSalaries();


      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", 3000, "IT");
      Employee employee2 = Employee.create("Rose", 4000, "Admin");
      Employee employee3 = Employee.create("Denise", 1500, "Sales");
      Employee employee4 = Employee.create("Mike", 2000, "IT");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.persist(employee4);
      em.getTransaction().commit();
      em.close();
  }

  private static void findEmployeeSalaries() {
      System.out.println("-- Employee names and salaries --");
      EntityManager em = entityManagerFactory.createEntityManager();
      TypedQuery<Tuple> query = em.createQuery("SELECT e.name, e.salary FROM Employee e", Tuple.class);
      List<Tuple> resultList = query.getResultList();
      resultList.forEach(tuple -> System.out.printf("name: %s, salary: %s%n", tuple.get(0),
              tuple.get(1, Long.class)));

  }
}
-- Employee names and salaries --
name: Diana, salary: 3000
name: Rose, salary: 4000
name: Denise, salary: 1500
name: Mike, salary: 2000

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.2.13.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.5.4

Tuple example in JPQL Select All Download
  • jpa-tuple-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources
            • META-INF

    See Also