【问题标题】:Join multiple table using criteria使用条件连接多个表
【发布时间】:2022-01-08 21:04:51
【问题描述】:

我有三个实体(我不显示构造函数和变量以免拉长整体)。第一个:

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_service_booking_request", unique = true, nullable = false)
public Integer getIdServiceBookingRequest() {
    return idServiceBookingRequest;
}

public void setIdServiceBookingRequest(Integer idServiceBookingRequest) {
    this.idServiceBookingRequest = idServiceBookingRequest;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_supplier_service")
public SupplierServiceTO getSupplierService() {
    return supplierService;
}

public void setSupplierService(SupplierServiceTO supplierService) {
    this.supplierService = supplierService;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_insert", length = 19)
public Date getDateInsert() {
    return dateInsert;
}

public void setDateInsert(Date dateInsert) {
    this.dateInsert = dateInsert;
}

@Column(name = "client_notes")
public String getClientNotes() {
    return clientNotes;
}

public void setClientNotes(String clientNotes) {
    this.clientNotes = clientNotes;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_senior")
public AnagraphicTO getSeniorTO() {
    return seniorTO;
}

public void setSeniorTO(AnagraphicTO seniorTO) {
    this.seniorTO = seniorTO;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_supplier_user")
public AnagraphicTO getSupplierUserTO() {
    return supplierUserTO;
}

public void setSupplierUserTO(AnagraphicTO supplierUserTO) {
    this.supplierUserTO = supplierUserTO;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_type_service_booking_status")
public TypeServiceBookingStatusTO getTypeServiceBookingStatusTO() {
    return typeServiceBookingStatusTO;
}

public void setTypeServiceBookingStatusTO(TypeServiceBookingStatusTO typeServiceBookingStatusTO) {
    this.typeServiceBookingStatusTO = typeServiceBookingStatusTO;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "final_time_slot")
public ServiceBookingTimeslotRequestTO getFinalServiceBookingTimeslotTO() {
    return finalServiceBookingTimeslotTO;
}

public void setFinalServiceBookingTimeslotTO(ServiceBookingTimeslotRequestTO finalServiceBookingTimeslotTO) {
    this.finalServiceBookingTimeslotTO = finalServiceBookingTimeslotTO;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "appointment_date", length = 19)
public Date getAppointmentDate() {
    return appointmentDate;
}

public void setAppointmentDate(Date appointmentDate) {
    this.appointmentDate = appointmentDate;
}

@Column(name = "supplier_notes")
public String getSupplierNotes() {
    return supplierNotes;
}

public void setSupplierNotes(String supplierNotes) {
    this.supplierNotes = supplierNotes;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_last_supplier_edit")
public AnagraphicTO getLastSupplierEdit() {
    return lastSupplierEdit;
}

public void setLastSupplierEdit(AnagraphicTO lastSupplierEdit) {
    this.lastSupplierEdit = lastSupplierEdit;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_update", length = 19)
public Date getLastUpdate() {
    return lastUpdate;
}

public void setLastUpdate(Date lastUpdate) {
    this.lastUpdate = lastUpdate;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "serviceBookingRequestTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<ServiceBookingTimeslotRequestTO> getServiceBookingTimeslotRequestTOs() {
    return serviceBookingTimeslotRequestTOs;
}

public void setServiceBookingTimeslotRequestTOs(
        Set<ServiceBookingTimeslotRequestTO> serviceBookingTimeslotRequestTOs) {
    this.serviceBookingTimeslotRequestTOs = serviceBookingTimeslotRequestTOs;
}
}

第二个:

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_supplier_service", unique = true, nullable = false)
public Integer getIdSupplierService() {
    return idSupplierService;
}

public void setIdSupplierService(Integer idSupplierService) {
    this.idSupplierService = idSupplierService;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_structure")
public StructureTO getStructureTO() {
    return structureTO;
}

public void setStructureTO(StructureTO structureTO) {
    this.structureTO = structureTO;
}

@Column(name = "price")
public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

@Column(name = "description")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Column(name = "radius")
public String getRadius() {
    return radius;
}

public void setRadius(String radius) {
    this.radius = radius;
}

@Column(name = "brief_description")
public String getBriefDescription() {
    return briefDescription;
}

public void setBriefDescription(String briefDescription) {
    this.briefDescription = briefDescription;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "start_date", length = 19)
public Date getDateStart() {
    return dateStart;
}

public void setDateStart(Date dateStart) {
    this.dateStart = dateStart;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_date", length = 19)
public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_type_service")
public TypeServiceTO getTypeServiceTO() {
    return typeServiceTO;
}

public void setTypeServiceTO(TypeServiceTO typeServiceTO) {
    this.typeServiceTO = typeServiceTO;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_last_editor")
public AnagraphicTO getLastEditor() {
    return lastEditor;
}

public void setLastEditor(AnagraphicTO lastEditor) {
    this.lastEditor = lastEditor;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "supplierServiceTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<ServiceSupplierTimeslotTO> getServiceSupplierTimeslotTOs() {
    return serviceSupplierTimeslotTOs;
}

public void setServiceSupplierTimeslotTOs(Set<ServiceSupplierTimeslotTO> serviceSupplierTimeslotTOs) {
    this.serviceSupplierTimeslotTOs = serviceSupplierTimeslotTOs;
}

最后一个:

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_structure", unique = true, nullable = false)
public Integer getIdStructure() {
    return this.idStructure;
}

public void setIdStructure(Integer idStructure) {
    this.idStructure = idStructure;
}

@Column(name = "id_structure_open_am", nullable = false, length = 50)
public String getIdStructureOpenAm() {
    return this.idStructureOpenAm;
}

public void setIdStructureOpenAm(String idStructureOpenAm) {
    this.idStructureOpenAm = idStructureOpenAm;
}

@Column(name = "description", nullable = false, length = 100)
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

@Column(name = "note", nullable = false)
public String getNote() {
    return this.note;
}

public void setNote(String note) {
    this.note = note;
}

@Column(name = "structure_phone_number", nullable = false)
public String getStructurePhoneNumber() {
    return structurePhoneNumber;
}

public void setStructurePhoneNumber(String structurePhoneNumber) {
    this.structurePhoneNumber = structurePhoneNumber;
}

@Column(name = "structure_email", nullable = false)
public String getStructureEmail() {
    return structureEmail;
}

public void setStructureEmail(String structureEmail) {
    this.structureEmail = structureEmail;
}
@Column(name = "tech_support_phone_number", nullable = false)
public String getTechSupportPhoneNumber() {
    return techSupportPhoneNumber;
}

public void setTechSupportPhoneNumber(String techSupportPhoneNumber) {
    this.techSupportPhoneNumber = techSupportPhoneNumber;
}
@Column(name = "faq_link", nullable = false)
public String getFaqLink() {
    return faqLink;
}

public void setFaqLink(String faqLink) {
    this.faqLink = faqLink;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<KitTO> getKitTOs() {
    return this.kitTOs;
}

public void setKitTOs(Set<KitTO> kitTOs) {
    this.kitTOs = kitTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AnagraphicStructureTO> getAnagraphicStructureTOs() {
    return this.anagraphicStructureTOs;
}

public void setAnagraphicStructureTOs(Set<AnagraphicStructureTO> anagraphicStructureTOs) {
    this.anagraphicStructureTOs = anagraphicStructureTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<InterventionTO> getInterventionTOs() {
    return this.interventionTOs;
}

public void setInterventionTOs(Set<InterventionTO> interventionTOs) {
    this.interventionTOs = interventionTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AssetTO> getAssetTOs() {
    return this.assetTOs;
}

public void setAssetTOs(Set<AssetTO> assetTOs) {
    this.assetTOs = assetTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AssetProviderTO> getAssetProviderTOs() {
    return this.assetProviderTOs;
}

public void setAssetProviderTOs(Set<AssetProviderTO> assetProviderTOs) {
    this.assetProviderTOs = assetProviderTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AssetModelTO> getAssetModelTOs() {
    return this.assetModelTOs;
}

public void setAssetModelTOs(Set<AssetModelTO> assetModelTOs) {
    this.assetModelTOs = assetModelTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AnagraphicTO> getAnagraphicTOs() {
    return this.anagraphicTOs;
}

public void setAnagraphicTOs(Set<AnagraphicTO> anagraphicTOs) {
    this.anagraphicTOs = anagraphicTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<AidTO> getAidTOs() {
    return this.aidTOs;
}

public void setAidTOs(Set<AidTO> aidTOs) {
    this.aidTOs = aidTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<TimeSlotsTO> getTimeSlotsTOs() {
    return this.timeSlotsTOs;
}

public void setTimeSlotsTOs(Set<TimeSlotsTO> timeSlotsTOs) {
    this.timeSlotsTOs = timeSlotsTOs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<ProfileTO> getProfileTOs() {
    return this.profileTOs;
}

public void setProfileTOs(Set<ProfileTO> profileTOs) {
    this.profileTOs = profileTOs;
}

@Column(name = "company_name", length = 100)
public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

@Column(name = "partita_iva", length = 100)
public String getPartitaIva() {
    return partitaIva;
}

public void setPartitaIva(String partitaIva) {
    this.partitaIva = partitaIva;
}

@Column(name = "rea", length = 100)
public String getRea() {
    return rea;
}

public void setRea(String rea) {
    this.rea = rea;
}

@Column(name = "pec", length = 100)
public String getPec() {
    return pec;
}

public void setPec(String pec) {
    this.pec = pec;
}

@Column(name = "address", length = 100)
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

@Column(name = "business_name", length = 100)
public String getBusinessName() {
    return businessName;
}

public void setBusinessName(String businessName) {
    this.businessName = businessName;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_type_structure", nullable = false)
public TypeStructureTO getTypeStructureTO() {
    return typeStructureTO;
}

public void setTypeStructureTO(TypeStructureTO typeStructureTO) {
    this.typeStructureTO = typeStructureTO;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_type_supplier_category", nullable = false)
public TypeSupplierCategoryTO getTypeSupplierCategoryTO() {
    return typeSupplierCategoryTO;
}

public void setTypeSupplierCategoryTO(TypeSupplierCategoryTO typeSupplierCategoryTO) {
    this.typeSupplierCategoryTO = typeSupplierCategoryTO;
}

@Column(name = "active")
public Boolean getActive() {
    return active;
}

public void setActive(Boolean active) {
    this.active = active;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "registration_date", nullable = false, length = 19)
public Date getRegistrationDate() {
    return registrationDate;
}

public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_date", nullable = false, length = 19)
public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.structureTO", cascade = {
        javax.persistence.CascadeType.ALL }, orphanRemoval = true)
public Set<StructureSupplierTO> getStructureSupplierTOs() {
    return structureSupplierTOs;
}

public void setStructureSupplierTOs(Set<StructureSupplierTO> structureSupplierTOs) {
    this.structureSupplierTOs = structureSupplierTOs;
}

我应该做一个查询,允许我选择最后一个实体的表的 id_structure,所以我这样继续:

public List<ServiceBookingRequestTO> findByStructure(Integer structureId) throws Throwable {
    try {
        Criteria criteria = this.getSession().createCriteria(ServiceBookingRequestTO.class);
        Criterion findStructure = Restrictions.eq("supplierService.structureTO.idStructure", structureId);
        criteria.add(findStructure);
        return criteria.list();
    } catch (Throwable e) {
        // TODO: handle exception
        e.printStackTrace();
        throw e;
    }
}

但是这个异常被捕获了:

org.hibernate.QueryException: could not resolve property: supplierService.structureTO.idStructure of: it.a.to.mad.m.ServiceBookingRequestTO

我做错了什么?

【问题讨论】:

    标签: java sql hibernate criteria


    【解决方案1】:

    我相信您只需要在限制中提供属性名称。 这行得通吗?

    Restrictions.eq("idStructure", structureId)
    

    【讨论】:

    • 不,我试过了,但它一直给我同样的异常,我猜为什么 idStructure 是我试图查看的实体的一部分
    • 错误描述一模一样一样吗?这很奇怪,因为“supplierService.serviceTO”来自我们编辑的字符串。在我们删除它后,我看不到它如何出现在异常消息中。
    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多