Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is workaround, which is using canonical metamodel.</p> <p>Unfortunately EntityModel we get from productSearchFilterRoot does not help us much, because it only gives us view as a set. So we cannot query Attributes of IdClass right away by name of attribute from there. But there they are anyway: </p> <pre><code>//following will contain three attributes that are part of id. Set&lt;SingularAttribute&lt;? super ProductSearchFilter, ?&gt;&gt; s = model.getIdClassAttributes(); </code></pre> <p>Instead we we will go for canonical metamodel. For that we need one new class which we can implement by ourselves, or let Hibenate do it:</p> <pre><code>@StaticMetamodel(ProductSearchFilter.class) public abstract class ProductSearchFilter_ { public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; category; public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; fdaStatus; public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; source; public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; name; public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; searchColumn; public static volatile SingularAttribute&lt;ProductSearchFilter, String&gt; productId; } </code></pre> <p>And then we will use fields of ProductSearchFilter_ as argument to get in productSearchFilterRoot:</p> <pre><code>CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery&lt;Tuple&gt; query = queryBuilder.createTupleQuery(); Root&lt;ProductSearchFilter&gt; productSearchFilterRoot = query.from(ProductSearchFilter.class); query.multiselect( productSearchFilterRoot.get(ProductSearchFilter_.productId).alias("productId"), productSearchFilterRoot.get(ProductSearchFilter_.category).alias("category"), productSearchFilterRoot.get(ProductSearchFilter_.name).alias("name"), productSearchFilterRoot.get(ProductSearchFilter_.fdaStatus).alias("fdaStatus")); query.distinct(true); </code></pre> <p>Because we now have metamodel, I used it also to create selection for name, but because that one is not part of id, we could have left it as it was:</p> <pre><code>productSearchFilterRoot.get("name").alias("name"), </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload