Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA criteriabuilder "IN" predicate on foreign key error: Object comparisons can only use the equal() or notEqual() operators
    primarykey
    data
    text
    <p>I want to select a list of file references from a table by looking at which users have the rights to retrieve that file. To do this I have 3 tables, a file table, an access control table, and a users table.</p> <p>I am using JPA and Criteriabuilder (because there are more tables involved and I need dynamicle create the query, I am leaving out the other tables and predicates from this question for the sake of readability). </p> <p>The following code works</p> <pre><code>CriteriaBuilder queryBuilder = em.getCriteriaBuilder(); CriteriaQuery&lt;File&gt; queryDefinition = queryBuilder.createQuery(File.class); Root&lt;File&gt; FileRoot = queryDefinition.from(File.class); List&lt;Predicate&gt; predicateList = new ArrayList&lt;Predicate&gt;(); Predicate userPredicate = FileRoot .join("FileAccesControlCollection").join("userId").get("usersId").in(loggedInUser.getUsersId()); predicateList.add(userPredicate ); queryDefinition.where(predicateArray).distinct(true); Query q = em.createQuery(queryDefinition); List&lt;Files&gt; results = (List&lt;Files&gt;) q.getResultList(); </code></pre> <p>For the userpredicate I want to leave out the last join to the users table because the ID that I want to filter on is already present in the FileAccesControlCollection table, and a join is a computational expensive database operation.</p> <p>What I tried is to do this:</p> <pre><code>Predicate userPredicate = FileRoot .join("FileAccesControlCollection").get("usersId").in(loggedInUser.getUsersId()); </code></pre> <p>But I guess because the userId value in the FileAccesControlCollection entity class is a foreignkey reference to the Users class I get the following error:</p> <pre><code>Exception Description: Object comparisons can only use the equal() or notEqual() operators. Other comparisons must be done through query keys or direct attribute level comparisons. </code></pre> <p>Is there a way, using the loggedInUser entity or its Id, to filter the files by just joining the File class to the FileAccesControlCollection class and filtering on the userId foreign key? I am kind of new to JPA and using google lead me to a lot of pages but not a clear answer for something which seems to me should be possible. </p>
    singulars
    1. This table or related slice is empty.
    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.
    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