'JPA Criteria API - How to use EXISTS in a column

I know about the use of EXISTS and SubQuery in the WHERE clause. But what I would like to do is to use a SubQuery as a column. Like this: select exists(select 1 from tab2 B where B.tab1_id = A.id) from tab1 A

The count(*) of a subquery does something similiar with this:

Subquery<Long> subQueryTab2 = cQuery.subquery(Long.class);
Root<Tab2> tab2 = subQueryTab2.from(Tab2.class);
Expression<Long> tab2Count = cBuilder.count(tab2);
subQueryTab2.select(tab2Count);
subQueryTab2.where(cBuilder.equal(tab2.get("tab1"), tab1));   

And then I can use subQueryTab2.getSelection() as a column in the main query. But instead of count(*) SubQuery I would like to make an EXISTS SubQuery and return a boolean to use as a column in the main query.

Thanks in advance!



Sources

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

Source: Stack Overflow

Solution Source