'List elements matching collection of constraints using JPA

I have an entity defined like this in PostgreSQL (http://sqlfiddle.com/#!17/e5a2ec/1):

create table foo (
  id bigint primary key,
  code varchar(4) not null,
  label varchar(255) not null,
  from_date date not null,
  to_date date null,
  unique (code, from_date)
);

I can get the most advanced elements (with max from_date) matching a collection of code using native PostgreSQL query...

select f1.* 
from 
  foo f1,
  (select code, max(from_date) max_from_date from foo where code in ('a', 'b', 'c') group by code) f2
where f1.code = f2.code and f1.from_date = f2.max_from_date;

...but I'm not able to create a similar query using CriteriaQuery or JPQL.

My problem is I understood subqueries can only be used in ̀whereclause and cannot usemultiselect`.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source