0

我有两个表(ProspectMaster,ProspectContactsMaster)。为了维持关系,我使用可嵌入实体(ProspectContactsMapID)制作了一个映射表(ProspectContactsMap)。下面是所有实体

@Entity
public class ProspectMaster {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int prospectID;

    @Column(nullable=false)
    @Length(max = 50)
    private String companyName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "prospectContactsMapID.prospectMaster", cascade=CascadeType.ALL)
    private Set<ProspectContactsMap> prospectContactsMap = new HashSet<ProspectContactsMap>(0);

    public int getProspectID() {
        return prospectID;
    }

    public void setProspectID(int prospectID) {
        this.prospectID = prospectID;
    }

    public String getCompanyName() {
        return companyName;
    }

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

    public Set<ProspectContactsMap> getProspectContactsMap() {
        return prospectContactsMap;
    }

    public void setProspectContactsMap(Set<ProspectContactsMap> prospectContactsMap) {
        this.prospectContactsMap = prospectContactsMap;
    }

}

@Entity
public class ProspectContactsMaster {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int prospectContactID;

    @Length(max = 25)
    private String firstName;

    public int getProspectContactID() {
        return prospectContactID;
    }

    public void setProspectContactID(int prospectContactID) {
        this.prospectContactID = prospectContactID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

@Entity
@AssociationOverrides({
    @AssociationOverride(name = "prospectContactsMapID.prospectMaster", 
        joinColumns = @JoinColumn(name = "prospectID")),
    @AssociationOverride(name = "prospectContactsMapID.prospectContactsMaster", 
        joinColumns = @JoinColumn(name = "prospectContactID")) })
public class ProspectContactsMap {

    @EmbeddedId
    private ProspectContactsMapID prospectContactsMapID = new ProspectContactsMapID();

    private int isMainContact;

    @Transient
    public ProspectMaster getProspectMaster() {
        return this.getProspectContactsMapID().getProspectMaster();
    }

    public void setProspectMaster(ProspectMaster prospectMaster) {
        getProspectContactsMapID().setProspectMaster(prospectMaster);
    }

    @Transient
    public ProspectContactsMaster getProspectContactsMaster() {
        return this.getProspectContactsMapID().getProspectContactsMaster();
    }

    public void setProspectContactsMaster(ProspectContactsMaster prospectContactsMaster) {
        getProspectContactsMapID().setProspectContactsMaster(prospectContactsMaster);
    }

    public ProspectContactsMapID getProspectContactsMapID() {
        return prospectContactsMapID;
    }

    public void setProspectContactsMapID(ProspectContactsMapID prospectContactsMapID) {
        this.prospectContactsMapID = prospectContactsMapID;
    }

    public int getIsMainContact() {
        return isMainContact;
    }

    public void setIsMainContact(int isMainContact) {
        this.isMainContact = isMainContact;
    }

}

@Embeddable
public class ProspectContactsMapID implements Serializable {
    @ManyToOne
    private ProspectMaster prospectMaster;

    @ManyToOne
    private ProspectContactsMaster prospectContactsMaster;

    public ProspectMaster getProspectMaster() {
        return prospectMaster;
    }

    public void setProspectMaster(ProspectMaster prospectMaster) {
        this.prospectMaster = prospectMaster;
    }

    public ProspectContactsMaster getProspectContactsMaster() {
        return prospectContactsMaster;
    }

    public void setProspectContactsMaster(ProspectContactsMaster prospectContactsMaster) {
        this.prospectContactsMaster = prospectContactsMaster;
    }
}

现在我的要求是获取公司名称以 d 开头或主要联系人以 d 开头的所有潜在客户。我已经进行了查询,但是在 n 级别我无法提供关联属性。贝娄是我的查询

Disjunction disjOrCondition = Restrictions.disjunction(); 
Criteria cr = getCurrentSession().createCriteria(ProspectMaster.class);
cr.createAlias("prospectContactsMap", "prospectContact");

Criterion thirdCondition = Restrictions.conjunction().add(Restrictions.eq("prospectContact.isMainContact",1))
    .add(Restrictions.like("prospectContact.prospectContactsMapID.prospectContactsMaster.firstName", searchVal.toString(),MatchMode.ANYWHERE));
disjOrCondition.add(Restrictions.like("companyName", searchVal.toString(),MatchMode.ANYWHERE));
disjOrCondition.add(thirdCondition);

如果我运行这个,我会得到以下异常

org.hibernate.QueryException: could not resolve property: prospectContactsMapID.prospectContactsMaster.firstName

谁能告诉我问题出在哪里?任何人都可以告诉我任何相同的例子。我只想使用 hql。

4

1 回答 1

0

据我所知,我只能告诉你两件事:

  • 您可以尝试另一种获取策略;你可以试试 EAGER 而不是 LAZY

  • 配置文件中有一个属性:hibernate.max_fetch_depth,但我猜你使用的是默认值。它设置单端关联的最大深度。

于 2013-08-27T10:13:09.683 回答