Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Criteria API: Select single column from one-to-many relationship table
    primarykey
    data
    text
    <p>I am trying to select a single column from a related table. I have a table (Item) with many Values. I would like to select Value.valueString. </p> <p>Basically, the query is supposed to pass in a bunch of values and pull any ValueFields that contain those values. The SQL might look something like this:</p> <pre><code>select ItemValues.valueString from ItemEntity join StockItem on ItemEntity.stockItemId = StockItem.id join ItemValues on ItemEntity.id = ItemValues.itemId where StockItem.vendor = vendorId AND (ItemValues.valueString like '%test%' OR ItemValues.valueString like '%test2%'...); </code></pre> <p>Here is my code:</p> <pre><code> final CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery&lt;String&gt; query = builder.createQuery(String.class); final Root&lt;ItemEntity&gt; root = query.from(ItemEntity.class); query.select(root.join("ItemValues").&lt;String&gt;get("ValueString")); final List&lt;Predicate&gt; filters = new LinkedList&lt;Predicate&gt;(); filters.add(builder.equal(root.join("StockItem").get("id"), vendorNumber)); final List&lt;Predicate&gt; filterNamesCriteria = new LinkedList&lt;Predicate&gt;(); if (filenames.length &gt; 0) { for (String fileName : filenames) { filterNamesCriteria.add(builder.like(root.join("ItemValues").&lt;String&gt;get("ValueString"), fileName)); } filters.add(builder.or(filterNamesCriteria.toArray(new Predicate[0]))); } query.where(filters.toArray(new Predicate[0])); final TypedQuery&lt;String&gt; resolvedQuery = this.entityManager.createQuery(query); return resolvedQuery.getResultList(); </code></pre> <p>I want the result to return a List of Strings (valueString column), but it's not returning anything.</p> <p>Am I doing something wrong? When I say "builder.createQuery(String.class)", is that correct?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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