2013-08-27 177 views
0

我有兩個表(ProspectMaster,ProspectContactsMaster)。爲了保持關係,我使用一個可嵌入的實體(ProspectContactsMapID)製作了一個映射表(ProspectContactsMap)。貝婁是所有實體休眠與n級關聯

@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。

+0

關於表格的問題:您是否在ProspectMaster表中爲ProspectContactsMap指定了一個外部標識? 我的猜測是「可嵌入」實體存在問題。如果你有外鍵,你不需要映射表,你可以使用一對多沒有它,沒有這個:'.add(Restrictions.like(「prospectContact.prospectContactsMapID.prospectContactsMaster.firstName」' –

+0

看,我的協會工作正常,導致插入沒有問題,只有在n深度關聯中獲取數據有問題。即使我的協會工作到第二級不超過 – RITUser

回答

0

從我知道我可以告訴你兩兩件事:

  • 你可以嘗試另一種抓取策略;而不是LAZY你可以嘗試EAGER

  • 在配置文件中有一個屬性:hibernate.max_fetch_depth,但我想你使用的是默認值。它爲單端分析設置了最大深度。