Figure 926. JPQL Slide presentation Create comment in forum
final Query query = entityManager.createQuery(
  "SELECT S FROM StudyCourse AS S WHERE S.shortName like 'C%'");
select studycours0_.shortName as shortNam1_0_,
       studycours0_.fullName as fullName2_0_
from StudyCourse studycours0_
where studycours0_.shortName like 'C%'

But wait: What about security?


Figure 927. JPQL parameter Slide presentation Create comment in forum
final Query query = entityManager.createQuery(
  "SELECT S FROM StudyCourse AS S WHERE S.shortName like :pattern");
query.setParameter("pattern", "C%");
select studycours0_.shortName as shortNam1_0_,
       studycours0_.fullName  as fullName2_0_
from   StudyCourse studycours0_
where  studycours0_.shortName like ?

Figure 928. JPQL record retrieval Slide presentation Create comment in forum
final Query queryRecords = entityManager.createQuery(
  "SELECT S.fullName, S.shortName FROM StudyCourse AS S" );
queryRecords.getResultList().stream().
  map(r -> {
    final Object[] record = (Object[]) r; 
    return (String)record[0] + " (" + (String)record[1] + ")" ;
  }).forEach(System.out::println) ;
Computer Science and Media (CSM) ...

Retrieving two attribute values fullName and shortName from StudyCourse records.

Note

Notice the ordering: fullName appears at first and shortName at second position.

Compile time prior knowledge: JPA wraps each database record into an Object[] array typically (albeit in the current example) containing heterogeneous instance references corresponding to database types being involved.

The array's size equals the number of database attributes being requested being 2 in the given example. fullName and shortName record values appear at array index positions 0 and 1 respectively.

Note

As with plain JDBC you are on your own handling types being involved accordingly. Obviously there is no compile time type checking whatsoever. instanceof may save you from troubles.

Digging into array values: Since both fullName and shortName are strings identical casts can be used.

Note

The two casts are used for illustration purposes: In the current example omitting these will have no effect at all: The Java runtime will call the overridden toString() method anyway.

Calling non-Object methods like e.g. toUpperCase() however requires a cast:

final Query queryRecords = entityManager.createQuery(
  "SELECT S.fullName, S.shortName FROM StudyCourse AS S" );

queryRecords.getResultList().stream().
  map(r -> {
    final Object[] record = (Object[]) r;
    return ((String)record[0]).toUpperCase() + " (" + (String)record[1] + ")" ;
  }).
  forEach(System.out::println) ;

Print values.