title: How to write criteria query involving an object and another related object of different class?
body: <p>My controller method</p>
  <pre><code>@Override
          public List&lt;SuggestedCompnayiesDTO&gt; filterSuggestedCompanies(Long partnerId, Boolean isPharmacy) {
              CriteriaBuilder builder = em.getCriteriaBuilder();
              CriteriaQuery&lt;SuggestedCompnayies&gt; query = builder.createQuery(SuggestedCompnayies.class);
              Root&lt;SuggestedCompnayies&gt; root = query.from(SuggestedCompnayies.class);
              List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
              if (partnerId != null) {
      
                  predicates.add(builder.equal(companyRepository.findById(root.get(&quot;partnerId&quot;), partnerId));
              }
      
      //This line here causes the problem
      
              if (isHospital != null) {
      
                  predicates.add(builder.equal(root.get(&quot;companyId&quot;).get(&quot;isHospital&quot;)), isHospital));
              }
              query.select(root).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).distinct(true);
              List&lt;SuggestedCompnayiesDTO&gt; suggestedCompnayiesDTO = suggestedCompnayiesMapper.toDto(em.createQuery(query).getResultList());
              
              return suggestedCompnayiesDTO;
          }
      }
  </code></pre>
  <p>Here I have an object of class <code>SuggestedCompanies</code> and it contains a field of type <code>Company</code>.My query need to involve checking conditions on both these objects.Ie I need to return only <code>SuggestedCompany</code> objects whose <code>partnerId</code> matches with the input and its inner object company's <code>isPharmacy</code> attribute set to true.
  I tried following piece of code</p>
  <pre><code>        predicates.add(builder.equal(root.get(&quot;companyId&quot;).get(&quot;isHospital&quot;)), isHospital));
  </code></pre>
  <p>But I get the error <code>Type mismatch: cannot convert from Path&lt;Object&gt; to Long</code>
  How can I solve this problem?</p>
  <p>Update</p>
  <p>Entity Classes</p>
  <pre><code>public class SuggestedCompnayies implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @ManyToOne
      @JsonIgnoreProperties(&quot;suggestedByComapnies&quot;)
      private Partner suggestedBy;
  
      @ManyToOne
      @JsonIgnoreProperties(&quot;suggestedToComapnies&quot;)
      private Partner suggestedTo;
  
      @ManyToOne
      @JsonIgnoreProperties(&quot;suggestedComapnies&quot;)
      private Company company;
  
      // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Partner getSuggestedBy() {
          return suggestedBy;
      }
  
      public SuggestedCompnayies suggestedBy(Partner partner) {
          this.suggestedBy = partner;
          return this;
      }
  
      public void setSuggestedBy(Partner partner) {
          this.suggestedBy = partner;
      }
  
      public Partner getSuggestedTo() {
          return suggestedTo;
      }
  
      public SuggestedCompnayies suggestedTo(Partner partner) {
          this.suggestedTo = partner;
          return this;
      }
  
      public void setSuggestedTo(Partner partner) {
          this.suggestedTo = partner;
      }
  
      public Company getCompany() {
          return company;
      }
  
      public SuggestedCompnayies company(Company company) {
          this.company = company;
          return this;
      }
  
      public void setCompany(Company company) {
          this.company = company;
      }
      // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
  
      @Override
      public boolean equals(Object o) {
          if (this == o) {
              return true;
          }
          if (!(o instanceof SuggestedCompnayies)) {
              return false;
          }
          return id != null &amp;&amp; id.equals(((SuggestedCompnayies) o).id);
      }
  
      @Override
      public int hashCode() {
          return 31;
      }
  
      @Override
      public String toString() {
          return &quot;SuggestedCompnayies{&quot; +
              &quot;id=&quot; + getId() +
              &quot;}&quot;;
      }
  }
  </code></pre>
  <p>Company</p>
  <pre><code>public class Company implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;name&quot;)
      private String name;
  
      @Column(name = &quot;email&quot;)
      private String email;
  
      @Column(name = &quot;location&quot;)
      private String location;
  
      @Column(name = &quot;google_map_location&quot;)
      private String googleMapLocation;
  
      @Column(name = &quot;longitude&quot;)
      private String longitude;
  
      @Column(name = &quot;latitude&quot;)
      private String latitude;
  
      @Column(name = &quot;street_1&quot;)
      private String street1;
  
      @Column(name = &quot;street_2&quot;)
      private String street2;
  
      @Column(name = &quot;city&quot;)
      private String city;
  
      @Column(name = &quot;zipcode&quot;)
      private Long zipcode;
  
      @Column(name = &quot;contact_number&quot;)
      private Long contactNumber;
  
      @Column(name = &quot;logo&quot;)
      private String logo;
  
      @Column(name = &quot;image&quot;)
      private String image;
  
      @Column(name = &quot;is_pharmacy&quot;)
      private Boolean isPharmacy;
  
      @Column(name = &quot;is_hospital&quot;)
      private Boolean isHospital;
  
      @Column(name = &quot;is_laboratory&quot;)
      private Boolean isLaboratory;
  
      @Column(name = &quot;is_distributor&quot;)
      private Boolean isDistributor;
  
      @Column(name = &quot;is_group_pharmacy&quot;)
      private Boolean isGroupPharmacy;
  
      @Column(name = &quot;created_by_id&quot;)
      private Long createdById;
  
      @Column(name = &quot;updated_by_id&quot;)
      private Long updatedById;
  
      @Column(name = &quot;created_on&quot;)
      private Instant createdOn;
  
      @Column(name = &quot;updated_on&quot;)
      private Instant updatedOn;
  
      @Column(name = &quot;is_approved&quot;)
      private Boolean isApproved;
  
      @Column(name = &quot;is_active&quot;)
      private Boolean isActive;
  
      @Column(name = &quot;user_id&quot;)
      private Long userId;
  
      @Column(name = &quot;username&quot;)
      private String username;
  
      @Column(name = &quot;password&quot;)
      private String password;
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;CompanySubscription&gt; subscriptions = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;DocumentType&gt; types = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;Review&gt; reviews = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;Rating&gt; ratings = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;WorkingHour&gt; workingHours = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;Gallery&gt; galleries = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;Company&gt; parentCompanies = new HashSet&lt;&gt;();
  
      @OneToMany(mappedBy = &quot;company&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      private Set&lt;SuggestedCompnayies&gt; suggestedComapnies = new HashSet&lt;&gt;();
  
      @ManyToOne
      @JsonIgnoreProperties(&quot;companies&quot;)
      private State state;
  
      @ManyToOne
      @JsonIgnoreProperties(&quot;parentCompanies&quot;)
      private Company company;
  
      @ManyToMany(mappedBy = &quot;companies&quot;)
      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
      @JsonIgnore
      private Set&lt;Partner&gt; partners = new HashSet&lt;&gt;();
  
      // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public Company name(String name) {
          this.name = name;
          return this;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getEmail() {
          return email;
      }
  
      public Company email(String email) {
          this.email = email;
          return this;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getLocation() {
          return location;
      }
  
      public Company location(String location) {
          this.location = location;
          return this;
      }
  
      public void setLocation(String location) {
          this.location = location;
      }
  
      public String getGoogleMapLocation() {
          return googleMapLocation;
      }
  
      public Company googleMapLocation(String googleMapLocation) {
          this.googleMapLocation = googleMapLocation;
          return this;
      }
  
      public void setGoogleMapLocation(String googleMapLocation) {
          this.googleMapLocation = googleMapLocation;
      }
  
      public String getLongitude() {
          return longitude;
      }
  
      public Company longitude(String longitude) {
          this.longitude = longitude;
          return this;
      }
  
      public void setLongitude(String longitude) {
          this.longitude = longitude;
      }
  
      public String getLatitude() {
          return latitude;
      }
  
      public Company latitude(String latitude) {
          this.latitude = latitude;
          return this;
      }
  
      public void setLatitude(String latitude) {
          this.latitude = latitude;
      }
  
      public String getStreet1() {
          return street1;
      }
  
      public Company street1(String street1) {
          this.street1 = street1;
          return this;
      }
  
      public void setStreet1(String street1) {
          this.street1 = street1;
      }
  
      public String getStreet2() {
          return street2;
      }
  
      public Company street2(String street2) {
          this.street2 = street2;
          return this;
      }
  
      public void setStreet2(String street2) {
          this.street2 = street2;
      }
  
      public String getCity() {
          return city;
      }
  
      public Company city(String city) {
          this.city = city;
          return this;
      }
  
      public void setCity(String city) {
          this.city = city;
      }
  
      public Long getZipcode() {
          return zipcode;
      }
  
      public Company zipcode(Long zipcode) {
          this.zipcode = zipcode;
          return this;
      }
  
      public void setZipcode(Long zipcode) {
          this.zipcode = zipcode;
      }
  
      public Long getContactNumber() {
          return contactNumber;
      }
  
      public Company contactNumber(Long contactNumber) {
          this.contactNumber = contactNumber;
          return this;
      }
  
      public void setContactNumber(Long contactNumber) {
          this.contactNumber = contactNumber;
      }
  
      public String getLogo() {
          return logo;
      }
  
      public Company logo(String logo) {
          this.logo = logo;
          return this;
      }
  
      public void setLogo(String logo) {
          this.logo = logo;
      }
  
      public String getImage() {
          return image;
      }
  
      public Company image(String image) {
          this.image = image;
          return this;
      }
  
      public void setImage(String image) {
          this.image = image;
      }
  
      public Boolean isIsPharmacy() {
          return isPharmacy;
      }
  
      public Company isPharmacy(Boolean isPharmacy) {
          this.isPharmacy = isPharmacy;
          return this;
      }
  
      public void setIsPharmacy(Boolean isPharmacy) {
          this.isPharmacy = isPharmacy;
      }
  
      public Boolean isIsHospital() {
          return isHospital;
      }
  
      public Company isHospital(Boolean isHospital) {
          this.isHospital = isHospital;
          return this;
      }
  
      public void setIsHospital(Boolean isHospital) {
          this.isHospital = isHospital;
      }
  
      public Boolean isIsLaboratory() {
          return isLaboratory;
      }
  
      public Company isLaboratory(Boolean isLaboratory) {
          this.isLaboratory = isLaboratory;
          return this;
      }
  
      public void setIsLaboratory(Boolean isLaboratory) {
          this.isLaboratory = isLaboratory;
      }
  
      public Boolean isIsDistributor() {
          return isDistributor;
      }
  
      public Company isDistributor(Boolean isDistributor) {
          this.isDistributor = isDistributor;
          return this;
      }
  
      public void setIsDistributor(Boolean isDistributor) {
          this.isDistributor = isDistributor;
      }
  
      public Boolean isIsGroupPharmacy() {
          return isGroupPharmacy;
      }
  
      public Company isGroupPharmacy(Boolean isGroupPharmacy) {
          this.isGroupPharmacy = isGroupPharmacy;
          return this;
      }
  
      public void setIsGroupPharmacy(Boolean isGroupPharmacy) {
          this.isGroupPharmacy = isGroupPharmacy;
      }
  
      public Long getCreatedById() {
          return createdById;
      }
  
      public Company createdById(Long createdById) {
          this.createdById = createdById;
          return this;
      }
  
      public void setCreatedById(Long createdById) {
          this.createdById = createdById;
      }
  
      public Long getUpdatedById() {
          return updatedById;
      }
  
      public Company updatedById(Long updatedById) {
          this.updatedById = updatedById;
          return this;
      }
  
      public void setUpdatedById(Long updatedById) {
          this.updatedById = updatedById;
      }
  
      public Instant getCreatedOn() {
          return createdOn;
      }
  
      public Company createdOn(Instant createdOn) {
          this.createdOn = createdOn;
          return this;
      }
  
      public void setCreatedOn(Instant createdOn) {
          this.createdOn = createdOn;
      }
  
      public Instant getUpdatedOn() {
          return updatedOn;
      }
  
      public Company updatedOn(Instant updatedOn) {
          this.updatedOn = updatedOn;
          return this;
      }
  
      public void setUpdatedOn(Instant updatedOn) {
          this.updatedOn = updatedOn;
      }
  
      public Boolean isIsApproved() {
          return isApproved;
      }
  
      public Company isApproved(Boolean isApproved) {
          this.isApproved = isApproved;
          return this;
      }
  
      public void setIsApproved(Boolean isApproved) {
          this.isApproved = isApproved;
      }
  
      public Boolean isIsActive() {
          return isActive;
      }
  
      public Company isActive(Boolean isActive) {
          this.isActive = isActive;
          return this;
      }
  
      public void setIsActive(Boolean isActive) {
          this.isActive = isActive;
      }
  
      public Long getUserId() {
          return userId;
      }
  
      public Company userId(Long userId) {
          this.userId = userId;
          return this;
      }
  
      public void setUserId(Long userId) {
          this.userId = userId;
      }
  
      public String getUsername() {
          return username;
      }
  
      public Company username(String username) {
          this.username = username;
          return this;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public Company password(String password) {
          this.password = password;
          return this;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public Set&lt;CompanySubscription&gt; getSubscriptions() {
          return subscriptions;
      }
  
      public Company subscriptions(Set&lt;CompanySubscription&gt; companySubscriptions) {
          this.subscriptions = companySubscriptions;
          return this;
      }
  
      public Company addSubscription(CompanySubscription companySubscription) {
          this.subscriptions.add(companySubscription);
          companySubscription.setCompany(this);
          return this;
      }
  
      public Company removeSubscription(CompanySubscription companySubscription) {
          this.subscriptions.remove(companySubscription);
          companySubscription.setCompany(null);
          return this;
      }
  
      public void setSubscriptions(Set&lt;CompanySubscription&gt; companySubscriptions) {
          this.subscriptions = companySubscriptions;
      }
  
      public Set&lt;DocumentType&gt; getTypes() {
          return types;
      }
  
      public Company types(Set&lt;DocumentType&gt; documentTypes) {
          this.types = documentTypes;
          return this;
      }
  
      public Company addTypes(DocumentType documentType) {
          this.types.add(documentType);
          documentType.setCompany(this);
          return this;
      }
  
      public Company removeTypes(DocumentType documentType) {
          this.types.remove(documentType);
          documentType.setCompany(null);
          return this;
      }
  
      public void setTypes(Set&lt;DocumentType&gt; documentTypes) {
          this.types = documentTypes;
      }
  
      public Set&lt;Review&gt; getReviews() {
          return reviews;
      }
  
      public Company reviews(Set&lt;Review&gt; reviews) {
          this.reviews = reviews;
          return this;
      }
  
      public Company addReviews(Review review) {
          this.reviews.add(review);
          review.setCompany(this);
          return this;
      }
  
      public Company removeReviews(Review review) {
          this.reviews.remove(review);
          review.setCompany(null);
          return this;
      }
  
      public void setReviews(Set&lt;Review&gt; reviews) {
          this.reviews = reviews;
      }
  
      public Set&lt;Rating&gt; getRatings() {
          return ratings;
      }
  
      public Company ratings(Set&lt;Rating&gt; ratings) {
          this.ratings = ratings;
          return this;
      }
  
      public Company addRatings(Rating rating) {
          this.ratings.add(rating);
          rating.setCompany(this);
          return this;
      }
  
      public Company removeRatings(Rating rating) {
          this.ratings.remove(rating);
          rating.setCompany(null);
          return this;
      }
  
      public void setRatings(Set&lt;Rating&gt; ratings) {
          this.ratings = ratings;
      }
  
      public Set&lt;WorkingHour&gt; getWorkingHours() {
          return workingHours;
      }
  
      public Company workingHours(Set&lt;WorkingHour&gt; workingHours) {
          this.workingHours = workingHours;
          return this;
      }
  
      public Company addWorkingHours(WorkingHour workingHour) {
          this.workingHours.add(workingHour);
          workingHour.setCompany(this);
          return this;
      }
  
      public Company removeWorkingHours(WorkingHour workingHour) {
          this.workingHours.remove(workingHour);
          workingHour.setCompany(null);
          return this;
      }
  
      public void setWorkingHours(Set&lt;WorkingHour&gt; workingHours) {
          this.workingHours = workingHours;
      }
  
      public Set&lt;Gallery&gt; getGalleries() {
          return galleries;
      }
  
      public Company galleries(Set&lt;Gallery&gt; galleries) {
          this.galleries = galleries;
          return this;
      }
  
      public Company addGallery(Gallery gallery) {
          this.galleries.add(gallery);
          gallery.setCompany(this);
          return this;
      }
  
      public Company removeGallery(Gallery gallery) {
          this.galleries.remove(gallery);
          gallery.setCompany(null);
          return this;
      }
  
      public void setGalleries(Set&lt;Gallery&gt; galleries) {
          this.galleries = galleries;
      }
  
      public Set&lt;Company&gt; getParentCompanies() {
          return parentCompanies;
      }
  
      public Company parentCompanies(Set&lt;Company&gt; companies) {
          this.parentCompanies = companies;
          return this;
      }
  
      public Company addParentCompany(Company company) {
          this.parentCompanies.add(company);
          company.setCompany(this);
          return this;
      }
  
      public Company removeParentCompany(Company company) {
          this.parentCompanies.remove(company);
          company.setCompany(null);
          return this;
      }
  
      public void setParentCompanies(Set&lt;Company&gt; companies) {
          this.parentCompanies = companies;
      }
  
      public Set&lt;SuggestedCompnayies&gt; getSuggestedComapnies() {
          return suggestedComapnies;
      }
  
      public Company suggestedComapnies(Set&lt;SuggestedCompnayies&gt; suggestedCompnayies) {
          this.suggestedComapnies = suggestedCompnayies;
          return this;
      }
  
      public Company addSuggestedComapnies(SuggestedCompnayies suggestedCompnayies) {
          this.suggestedComapnies.add(suggestedCompnayies);
          suggestedCompnayies.setCompany(this);
          return this;
      }
  
      public Company removeSuggestedComapnies(SuggestedCompnayies suggestedCompnayies) {
          this.suggestedComapnies.remove(suggestedCompnayies);
          suggestedCompnayies.setCompany(null);
          return this;
      }
  
      public void setSuggestedComapnies(Set&lt;SuggestedCompnayies&gt; suggestedCompnayies) {
          this.suggestedComapnies = suggestedCompnayies;
      }
  
      public State getState() {
          return state;
      }
  
      public Company state(State state) {
          this.state = state;
          return this;
      }
  
      public void setState(State state) {
          this.state = state;
      }
  
      public Company getCompany() {
          return company;
      }
  
      public Company company(Company company) {
          this.company = company;
          return this;
      }
  
      public void setCompany(Company company) {
          this.company = company;
      }
  
      public Set&lt;Partner&gt; getPartners() {
          return partners;
      }
  
      public Company partners(Set&lt;Partner&gt; partners) {
          this.partners = partners;
          return this;
      }
  
      public Company addPartners(Partner partner) {
          this.partners.add(partner);
          partner.getCompanies().add(this);
          return this;
      }
  
      public Company removePartners(Partner partner) {
          this.partners.remove(partner);
          partner.getCompanies().remove(this);
          return this;
      }
  
      public void setPartners(Set&lt;Partner&gt; partners) {
          this.partners = partners;
      }
      // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
  
      @Override
      public boolean equals(Object o) {
          if (this == o) {
              return true;
          }
          if (!(o instanceof Company)) {
              return false;
          }
          return id != null &amp;&amp; id.equals(((Company) o).id);
      }
  
      @Override
      public int hashCode() {
          return 31;
      }
  
      @Override
      public String toString() {
          return &quot;Company{&quot; +
              &quot;id=&quot; + getId() +
              &quot;, name='&quot; + getName() + &quot;'&quot; +
              &quot;, email='&quot; + getEmail() + &quot;'&quot; +
              &quot;, location='&quot; + getLocation() + &quot;'&quot; +
              &quot;, googleMapLocation='&quot; + getGoogleMapLocation() + &quot;'&quot; +
              &quot;, longitude='&quot; + getLongitude() + &quot;'&quot; +
              &quot;, latitude='&quot; + getLatitude() + &quot;'&quot; +
              &quot;, street1='&quot; + getStreet1() + &quot;'&quot; +
              &quot;, street2='&quot; + getStreet2() + &quot;'&quot; +
              &quot;, city='&quot; + getCity() + &quot;'&quot; +
              &quot;, zipcode=&quot; + getZipcode() +
              &quot;, contactNumber=&quot; + getContactNumber() +
              &quot;, logo='&quot; + getLogo() + &quot;'&quot; +
              &quot;, image='&quot; + getImage() + &quot;'&quot; +
              &quot;, isPharmacy='&quot; + isIsPharmacy() + &quot;'&quot; +
              &quot;, isHospital='&quot; + isIsHospital() + &quot;'&quot; +
              &quot;, isLaboratory='&quot; + isIsLaboratory() + &quot;'&quot; +
              &quot;, isDistributor='&quot; + isIsDistributor() + &quot;'&quot; +
              &quot;, isGroupPharmacy='&quot; + isIsGroupPharmacy() + &quot;'&quot; +
              &quot;, createdById=&quot; + getCreatedById() +
              &quot;, updatedById=&quot; + getUpdatedById() +
              &quot;, createdOn='&quot; + getCreatedOn() + &quot;'&quot; +
              &quot;, updatedOn='&quot; + getUpdatedOn() + &quot;'&quot; +
              &quot;, isApproved='&quot; + isIsApproved() + &quot;'&quot; +
              &quot;, isActive='&quot; + isIsActive() + &quot;'&quot; +
              &quot;, userId=&quot; + getUserId() +
              &quot;, username='&quot; + getUsername() + &quot;'&quot; +
              &quot;, password='&quot; + getPassword() + &quot;'&quot; +
              &quot;}&quot;;
      }
  }
  </code></pre>