title: org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatibile
body: <p>I have a many-to-many relationship in JPA 2.0 provided by <s>Hibernate 4.2.0 CR1</s> (recently upgraded to Hibernate 4.2.7 final) between <code>Product</code> and <code>Colour</code> as follows.</p>
  
  <p>The <code>Product</code> entity class:</p>
  
  <pre><code>public class Product implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "prod_id")
      private Long prodId;
  
      @JoinTable(name = "prod_colour", joinColumns = {
          @JoinColumn(name = "prod_id", referencedColumnName = "prod_id")}, inverseJoinColumns = {
          @JoinColumn(name = "colour_id", referencedColumnName = "colour_id")})
      @ManyToMany(fetch = FetchType.EAGER)
  
      private Set&lt;Colour&gt; colourSet;
  
      //Setters and getters.
  }
  </code></pre>
  
  <p>The <code>Colour</code> entity class.</p>
  
  <pre><code>public class Colour implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "colour_id")
      private Long colourId;
  
      @ManyToMany(mappedBy = "colourSet", fetch = FetchType.EAGER)
  
      private Set&lt;Product&gt; productSet;
  
      //Setters and getters.
  }
  </code></pre>
  
  <p>To fetch a list of colours from the <code>colour</code> table based on <code>prodId</code> supplied which does not match the colours in the <code>prod_colour</code> table (join table), I'm executing the following criteria query.</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;Colour&gt;criteriaQuery=criteriaBuilder.createQuery(Colour.class);
  Metamodel metamodel = entityManager.getMetamodel();
  EntityType&lt;Colour&gt; entityType = metamodel.entity(Colour.class);
  Root&lt;Colour&gt; root = criteriaQuery.from(entityType);
  criteriaQuery.select(root);
  
  Subquery&lt;Long&gt;subquery=criteriaQuery.subquery(Long.class);
  Root&lt;Product&gt; subRoot = subquery.from(Product.class);
  subquery.select(root.get(Colour_.colourId));
  
  Predicate paramPredicate = criteriaBuilder.equal(subRoot.get(Product_.prodId), prodId);
  Predicate correlatePredicate = criteriaBuilder.equal(root.get(Colour_.productSet), subRoot);
  
  subquery.where(criteriaBuilder.and(paramPredicate, correlatePredicate));
  criteriaQuery.where(criteriaBuilder.in(root.get(Colour_.colourId)).value(subquery).not());
  
  criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Colour_.colourId)));
  TypedQuery&lt;Colour&gt; typedQuery = entityManager.createQuery(criteriaQuery);
  List&lt;Colour&gt;list=typedQuery.getResultList();
  </code></pre>
  
  <p>This criteria query causes the following exception to be thrown.</p>
  
  <blockquote>
    <p>org.hibernate.TypeMismatchException: left and right hand sides of a
    binary logic operator were incompatibile
    [java.util.Set(model.Colour.productSet) : model.Product]</p>
  </blockquote>
  
  <p>The following JPQL however works.</p>
  
  <pre><code>FROM Colour colour 
  WHERE colour.colourId 
  NOT IN(
  SELECT colours.colourId 
       FROM Product product 
       INNER JOIN product.colourSet colours 
       WHERE product.prodId=:id) 
  ORDER BY colour.colourId DESC
  </code></pre>
  
  <p>What is the cause of this exception. How to make this criteria query work? This same criteria <a href="https://stackoverflow.com/a/19861861/1391249">query</a> somehow works in JPA provided by EclipseLink.</p>
  
  <hr>
  
  <p>The stacktrace : </p>
  
  <pre><code>SEVERE: Error Rendering View[/admin_side/ProductColour.xhtml]
  javax.el.ELException: /admin_side/ProductColour.xhtml @28,121 rendered="#{not empty productColourManagedBean.colourList}": Error reading 'colourList' on type admin.mangedbean.ProductColourManagedBean
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:457)
      at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:63)
      at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:57)
      at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:204)
      at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:121)
      at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:58)
      at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
      at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
      at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:70)
      at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:57)
      at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:51)
      at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at filter.NoCacheFilter.doFilter(NoCacheFilter.java:27)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:150)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.access.channel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:144)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: javax.el.ELException: Error reading 'colourList' on type admin.mangedbean.ProductColourManagedBean
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:98)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
      at org.apache.el.parser.AstEmpty.getValue(AstEmpty.java:47)
      at org.apache.el.parser.AstNot.getValue(AstNot.java:44)
      at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      ... 82 more
  Caused by: javax.persistence.PersistenceException: org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatibile [java.util.Set(model.Colour.productSet) : model.Product]
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
      at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:470)
      at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:221)
      at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:587)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
      at $Proxy98.createQuery(Unknown Source)
      at admin.dao.ProductColourDAO.getList(ProductColourDAO.java:143)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
      at $Proxy120.getList(Unknown Source)
      at admin.mangedbean.ProductColourManagedBean.getColourList(ProductColourManagedBean.java:61)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
      ... 89 more
  Caused by: org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatibile [java.util.Set(model.Colour.productSet) : model.Product]
      at org.hibernate.hql.internal.ast.tree.BinaryLogicOperatorNode.mutateRowValueConstructorSyntaxesIfNecessary(BinaryLogicOperatorNode.java:89)
      at org.hibernate.hql.internal.ast.tree.BinaryLogicOperatorNode.initialize(BinaryLogicOperatorNode.java:78)
      at org.hibernate.hql.internal.ast.HqlSqlWalker.prepareLogicOperator(HqlSqlWalker.java:1224)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4371)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2076)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2004)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:4644)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4347)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2076)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2051)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
      at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:105)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:80)
      at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
      at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
      at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
      at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1734)
      at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:452)
      ... 119 more
  </code></pre>
  
  <p>Anyway, the <strong>criteria query</strong> should fetch a list of colours from the <code>colour</code> table based on <code>prodId</code> supplied which does not match the colours in the <code>prod_colour</code> table (It could be a join query, a subquery or anything else).</p>