title: I'm receiving an error of "type mismatch" in Hibernate's Criteria API when querying the database and don't know its cause
body: <p>I have to develop an application for searching users into the database with Spring and Hibernate. For this I headed for the Criteria API, which is renown for its flexibility it brings to working with queries for databases.
  I encounter over an error when I'm trying to design a query and send it to the DB, stating that:</p>
  <pre><code>&quot;status&quot;: 500,
  &quot;error&quot;: &quot;Internal Server Error&quot;,
  &quot;trace&quot;: &quot;java.lang.IllegalArgumentException: Parameter value [%ADMIN%] did not match expected type [com.ferendin.recruit.enums.RoleEnum (n/a)]\r\n\tat org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54)\r\n\tat org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27)\r\n\tat org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90)\r\n\tat org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55)\r\n\tat org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:494)\r\n\tat org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:115)\r\n\tat org.hibernate.query.criteria.internal.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:135)\r\n\tat org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:364)\r\n\tat org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:165)\r\n\tat org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:751)\r\n\tat org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114)\r\n\tat jdk.internal.reflect.GeneratedMethodAccessor138.invoke(Unknown Source)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\r\n\tat org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362)\r\n\tat com.sun.proxy.$Proxy449.createQuery(Unknown Source)\r\n\tat com.ferendin.recruit.services.UserServiceImpl.searchUsers(UserServiceImpl.java:122)\r\n\tat com.ferendin.recruit.services.UserServiceImpl$$FastClassBySpringCGLIB$$183fabe.invoke(&lt;generated&gt;)\r\n\tat org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)\r\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\r\n\tat org.springframework.aop.framework
  </code></pre>
  <p>The search() Spring endpoint is this:</p>
  <pre><code>@Override
  public List&lt;UserEntity&gt; searchUsers(UserSearchEntity searchCriteria)
  {
      log.info(&quot;UserServiceImpl -&gt; searchUsers&quot;);
      List&lt;UserEntity&gt; foundUsers;
              
      EntityManager entityManager = this.entityManagerFactory.createEntityManager();
      try {
          CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
          CriteriaQuery&lt;UserEntity&gt; criteriaQuery = criteriaBuilder.createQuery(UserEntity.class);
          Root&lt;UserEntity&gt; userz = criteriaQuery.from(UserEntity.class);
          Join&lt;UserEntity, RoleEntity&gt; comp = userz.join(&quot;roles&quot;, JoinType.INNER);
          //Join&lt;ProfileEntity, EducationEntity&gt; education = profile.join(&quot;studies&quot;, JoinType.INNER);
          //Join&lt;ProfileEntity, WorkExperienceEntity&gt; work = profile.join(&quot;workExperiences&quot;, JoinType.INNER);
          
          // make param list
          List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  
          // add predicates
          if (searchCriteria.getFirstName() != null) {
              predicates.add(criteriaBuilder.like(userz.get( &quot;firstName&quot; ), &quot;%&quot; + searchCriteria.getFirstName() + &quot;%&quot; ));
          }
          if (searchCriteria.getLastName() != null) {
              predicates.add(criteriaBuilder.like(userz.get( &quot;lastName&quot; ), &quot;%&quot; + searchCriteria.getLastName() + &quot;%&quot; ));
          }
          if (searchCriteria.getEmail() != null) {
              predicates.add(criteriaBuilder.like(userz.get( &quot;email&quot; ), &quot;%&quot;+searchCriteria.getEmail()+&quot;%&quot; ));
          }
          if (searchCriteria.getCompany() != null) {
              predicates.add(criteriaBuilder.like(userz.get( &quot;company&quot; ).get(&quot;name&quot;), searchCriteria.getCompany()));
          }
          if (searchCriteria.getRole() != null) {
              predicates.add(criteriaBuilder.like(userz.join(&quot;roles&quot;).get(&quot;name&quot;), &quot;%&quot;+searchCriteria.getRole()+&quot;%&quot;));
          }
          /*if (searchCriteria.getWillingRelocate() != null) {
              predicates.add(criteriaBuilder.equal(profile.get( &quot;willingToRelocate&quot; ),(searchCriteria.getWillingRelocate())));
          }
  
          if (searchCriteria.getIsGraduate() != null) {
              predicates.add(criteriaBuilder.isNotNull(education.get(&quot;dateUntil&quot;)));
          }
          if (searchCriteria.getYearsExperience() != null) {
              //predicates.add(work.get(&quot;yearsExperience&quot;).in(searchCriteria.getYearsExperience()));
              //aici mai trebuiesc facute niste calcule pentru a afla totalul de ani cati a lucrat userul
          }*/
  
          // query itself
          criteriaQuery.select(userz).where(predicates.toArray(new Predicate[]{}));
  
          // execute query and get results
          foundUsers = entityManager.createQuery(criteriaQuery).getResultList();
      } finally {
          if (entityManager != null) {
              entityManager.close();
          }
      }
      log.info(&quot;Users found&quot;);
      foundUsers.forEach(user -&gt; log.info(user.toString()));
      return foundUsers;
      
  }
  </code></pre>
  <p>All i can say is that it occurs when I try to search users into DB by their roles. For this I join the UserEntity with its corresp. RoleEntity by their connection field. This is the RoleEntity:</p>
  <pre><code>public class RoleEntity {
  @Id
  @GeneratedValue(generator = &quot;uuid&quot;)
  private UUID uuid;
  @Column(unique = true, nullable = false)
  @Enumerated(EnumType.STRING)
  private RoleEnum name;
  }
  </code></pre>
  <p>I'm aware that in my predicate (below)</p>
  <pre><code>if (searchCriteria.getRole() != null) {
              predicates.add(criteriaBuilder.like(userz.join(&quot;roles&quot;).get(&quot;name&quot;), &quot;%&quot;+searchCriteria.getRole()+&quot;%&quot;));
          }
  </code></pre>
  <p>I'm comparing a String with a RoleEnum (which has values: ROLE_ADMIN, ROLE_RECRUIT, ROLE_CANDIDATE), but I set the annotation EnumType.String for the field &quot;name&quot; in RoleEntity, so why is this not taken into account when I compare the 2 objects into my query?</p>