hibernate与n级关联

时间:2013-08-27 07:42:46

标签: java hibernate

我有两张桌子(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。

1 个答案:

答案 0 :(得分:0)

据我所知,我可以告诉你两件事:

  • 您可以尝试其他提取策略;你可以尝试EAGER

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