title: Spring data criteria query not returning results, but executing it manually does
body: <p>I'm trying to filter users by a field which is stored as a JSONB and mapped as a class. In order to achieve this, I created a query using a CriteriaBuilder (shown below). The result though is empty, but if I retrieve the executed query from the logs and run it manually on the database, it does retrieve the expected user.</p>
  <p>Field mapping:</p>
  <pre class="lang-java prettyprint-override"><code>@Type(type = &quot;JSONB&quot;)
      @Column(name = &quot;authentication_providers&quot;, nullable = false, columnDefinition = &quot;JSONB NOT NULL&quot;)
      private Set&lt;AuthenticationProvider&gt; authenticationProviders;
  </code></pre>
  <p>Query building:</p>
  <pre class="lang-java prettyprint-override"><code>final Expression&lt;?&gt; provider = criteriaBuilder.function(
          &quot;JSONB&quot;,
          String.class,
          criteriaBuilder.literal(authenticationProvider.toJsonString()) // authenticationProvider is a function argument with the info to filter with
  );
  
  return criteriaBuilder.isTrue(criteriaBuilder.function(
          &quot;jsonb_contains&quot;,
          Boolean.class,
          from.get(User_.authenticationProviders),
          provider
  ));
  </code></pre>
  <p>the toJsonString() method:</p>
  <pre class="lang-java prettyprint-override"><code>public String toJsonString() {
      return &quot;{\&quot;provider_name\&quot;:\&quot;&quot; + name.name() + &quot;\&quot;, \&quot;id\&quot;:\&quot;&quot; + id + &quot;\&quot;}&quot;;
  }
  </code></pre>
  <p>Executed query:</p>
  <pre class="lang-sql prettyprint-override"><code>select user0_.id as id1_4_, ... from public.users user0_ where jsonb_contains(user0_.authentication_providers,JSONB(?))=true
  </code></pre>
  <p>and the according to the logs, the binding is:</p>
  <pre><code>binding parameter [1] as [VARCHAR] - [{&quot;provider_name&quot;:&quot;GOOGLE&quot;, &quot;id&quot;:&quot;somenumber&quot;}]
  </code></pre>
  <p>Manually executed query:</p>
  <pre class="lang-sql prettyprint-override"><code>select user0_.id as id1_4_, ... from public.users user0_ where jsonb_contains(user0_.authentication_providers, JSONB('[{&quot;provider_name&quot;:&quot;GOOGLE&quot;, &quot;id&quot;:&quot;somenumber&quot;}]')) = true;
  </code></pre>
  <p>Any clues of what I'm doing wrong?</p>
  <p>Note: The '...' in the query represent several other fields that I did not include to make the code more readable.</p>
  <hr />
  <p>Edit:</p>
  <p>I did also attempt a different approach using @Query and I got the same problem, the query does not return any results, but manually it does work.</p>
  <p>repository:</p>
  <pre class="lang-java prettyprint-override"><code>@Query(nativeQuery = true, value = &quot;SELECT * FROM users WHERE authentication_providers @&gt; JSONB(:provider)&quot;)
      Optional&lt;User&gt; findByAuthenticationProvider(@Param(&quot;provider&quot;) final String authenticationProvider);
  </code></pre>
  <p>Executed query:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM users WHERE authentication_providers @&gt; JSONB(?)
  </code></pre>
  <p>Binding:</p>
  <pre><code>binding parameter [1] as [VARCHAR] - [{&quot;provider_name&quot;:&quot;GOOGLE&quot;, &quot;id&quot;:&quot;somenumber&quot;}]
  </code></pre>
  <p>Manually executed query:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM users WHERE authentication_providers @&gt; JSONB('[{&quot;provider_name&quot;:&quot;GOOGLE&quot;, &quot;id&quot;:&quot;somenumber&quot;}]');
  </code></pre>