我有两个表如下:
AFFILIATES
----------
affiliateId (String) (PK)
CONVERSION
-----------
conversionId (Integer) (PK)
purchaseId (Integer) (not null)
PURCHASE
---------
purchaseId (Integer) (PK)
associateId (String) (allow NULL)
实体映射如下:
@Entity
@Table(name="AFFILIATES")
class Affiliate implements Serializable {
@Id
@Column(name= "affiliate_id")
private String affiliateId;
@OneToMany(mappedBy="affiliate", fetch=FetchType.LAZY)
private Set<Conversion> conversion;
public String getAffiliateId() {
return affiliateId;
}
public void setAffiliateId(String affiliateId) {
this.affiliateId = affiliateId;
}
public Set<Conversion> getConversion() {
return conversion;
}
public void setConversion(Set<Conversion> conversion) {
this.conversion = conversion;
}
}
@Entity
@Table(name="CONVERSION")
class Conversion implements Serializable{
@Id
@Column(name="conversion_id")
private int conversionId;
@ManyToOne(optional=true, fetch=FetchType.LAZY)
private Affiliate affiliate;
public int getConversionId() {
return conversionId;
}
public void setConversionId(int conversionId) {
this.conversionId = conversionId;
}
public Affiliate getAffiliate() {
return affiliate;
}
public void setAffiliate(Affiliate affiliate) {
this.affiliate = affiliate;
}
}
现在我想获得给定的所有转换affiliateId
我有PURCHASE
两列,一列来自CONVERSION
,一列来自AFFILIATES
我尝试了以下代码,但它不起作用,
String hql = "from Affiliate as a where a.affiliateId = 'abc'";
Query q = s.createQuery(hql);
Affiliate a = (Affiliate) q.uniqueResult();
System.out.println(a);
a.getConversion().size();
我不知道如何获得affiliateID
abc 的所有转换。
如果您有任何想法,请告诉我。
应通过第三个表PURCHASE获取转换。