如何编写涉及一个对象和另一个不同类别的相关对象的条件查询?

huangapple go评论29阅读模式
英文:

How to write criteria query involving an object and another related object of different class?

问题

我的控制器方法

@Override
public List<SuggestedCompnayiesDTO> filterSuggestedCompanies(Long partnerId, Boolean isPharmacy) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<SuggestedCompnayies> query = builder.createQuery(SuggestedCompnayies.class);
    Root<SuggestedCompnayies> root = query.from(SuggestedCompnayies.class);
    List<Predicate> predicates = new ArrayList<>();
    if (partnerId != null) {

        predicates.add(builder.equal(companyRepository.findById(root.get("partnerId"), partnerId));
    }

    // 这一行引发了问题

    if (isHospital != null) {

        predicates.add(builder.equal(root.get("companyId").get("isHospital"), isHospital));
    }
    query.select(root).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).distinct(true);
    List<SuggestedCompnayiesDTO> suggestedCompnayiesDTO = suggestedCompnayiesMapper.toDto(em.createQuery(query).getResultList());

    return suggestedCompnayiesDTO;
}

在这里,我有一个SuggestedCompanies类的对象,它包含一个类型为Company的字段。我的查询需要涉及对这两个对象的条件检查。也就是说,我需要返回只有partnerId与输入匹配且其内部对象companyisPharmacy属性设置为true的SuggestedCompany对象。

我尝试了以下代码片段

predicates.add(builder.equal(root.get("companyId").get("isHospital"), isHospital));

但我得到错误类型不匹配:无法从Path<Object>转换为Long

我该如何解决这个问题?

更新

实体类

public class SuggestedCompnayies implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JsonIgnoreProperties("suggestedByComapnies")
    private Partner suggestedBy;

    @ManyToOne
    @JsonIgnoreProperties("suggestedToComapnies")
    private Partner suggestedTo;

    @ManyToOne
    @JsonIgnoreProperties("suggestedComapnies")
    private Company company;

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove

    // 其他属性和方法...

}

公司类

public class Company implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    // 其他属性和方法...

}

请注意,这只是代码的一部分,包括实体类的一部分属性和方法。如果需要完整的代码,请提供更多上下文。

英文:

My controller method

@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;
    	}
    }

Here I have an object of class SuggestedCompanies and it contains a field of type Company.My query need to involve checking conditions on both these objects.Ie I need to return only SuggestedCompany objects whose partnerId matches with the input and its inner object company's isPharmacy attribute set to true.
I tried following piece of code

		predicates.add(builder.equal(root.get(&quot;companyId&quot;).get(&quot;isHospital&quot;)), isHospital));

But I get the error Type mismatch: cannot convert from Path&lt;Object&gt; to Long
How can I solve this problem?

Update

Entity Classes

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;;
    }
}

Company

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=&#39;&quot; + getName() + &quot;&#39;&quot; +
&quot;, email=&#39;&quot; + getEmail() + &quot;&#39;&quot; +
&quot;, location=&#39;&quot; + getLocation() + &quot;&#39;&quot; +
&quot;, googleMapLocation=&#39;&quot; + getGoogleMapLocation() + &quot;&#39;&quot; +
&quot;, longitude=&#39;&quot; + getLongitude() + &quot;&#39;&quot; +
&quot;, latitude=&#39;&quot; + getLatitude() + &quot;&#39;&quot; +
&quot;, street1=&#39;&quot; + getStreet1() + &quot;&#39;&quot; +
&quot;, street2=&#39;&quot; + getStreet2() + &quot;&#39;&quot; +
&quot;, city=&#39;&quot; + getCity() + &quot;&#39;&quot; +
&quot;, zipcode=&quot; + getZipcode() +
&quot;, contactNumber=&quot; + getContactNumber() +
&quot;, logo=&#39;&quot; + getLogo() + &quot;&#39;&quot; +
&quot;, image=&#39;&quot; + getImage() + &quot;&#39;&quot; +
&quot;, isPharmacy=&#39;&quot; + isIsPharmacy() + &quot;&#39;&quot; +
&quot;, isHospital=&#39;&quot; + isIsHospital() + &quot;&#39;&quot; +
&quot;, isLaboratory=&#39;&quot; + isIsLaboratory() + &quot;&#39;&quot; +
&quot;, isDistributor=&#39;&quot; + isIsDistributor() + &quot;&#39;&quot; +
&quot;, isGroupPharmacy=&#39;&quot; + isIsGroupPharmacy() + &quot;&#39;&quot; +
&quot;, createdById=&quot; + getCreatedById() +
&quot;, updatedById=&quot; + getUpdatedById() +
&quot;, createdOn=&#39;&quot; + getCreatedOn() + &quot;&#39;&quot; +
&quot;, updatedOn=&#39;&quot; + getUpdatedOn() + &quot;&#39;&quot; +
&quot;, isApproved=&#39;&quot; + isIsApproved() + &quot;&#39;&quot; +
&quot;, isActive=&#39;&quot; + isIsActive() + &quot;&#39;&quot; +
&quot;, userId=&quot; + getUserId() +
&quot;, username=&#39;&quot; + getUsername() + &quot;&#39;&quot; +
&quot;, password=&#39;&quot; + getPassword() + &quot;&#39;&quot; +
&quot;}&quot;;
}
}

答案1

得分: 2

你应该先加入相关的实体,即首先与 Company 连接,然后将其属性作为条件添加。

Join<SuggestedCompanies, Company> company = root.join("company");
predicates.add(builder.equal(company.get("isHospital"), isHospital));

对于实体的属性,假设 partnerId 是属性,你可以这样做:

predicates.add(builder.equal(root.get("partnerId"), partnerId));
英文:

You should Join related entity first means join with Company first and then add as a condition on its property

Join&lt;SuggestedCompnayies, Company&gt; company = root.join(&quot;company&quot;);
predicates.add(builder.equal(company.get(&quot;isHospital&quot;), isHospital));

And for the entity's property suppose partnerId is property then you can do this way

predicates.add(builder.equal(root.get(&quot;partnerId&quot;), partnerId));

答案2

得分: 0

你可能错过了括号,尝试将其更改为:

predicates.add(builder.equal(root.get("companyId").get("isHospital"), isHospital));

英文:

Probably you have missed the bracket, try changing it to:

predicates.add(builder.equal(root.get(&quot;companyId&quot;)).get(&quot;isHospital&quot;), isHospital));

huangapple
  • 本文由 发表于 2020年8月2日 21:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定