0

我有两个表如下:

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();

我不知道如何获得affiliateIDabc 的所有转换。

如果您有任何想法,请告诉我。

应通过第三个表PURCHASE获取转换。

4

1 回答 1

0

我理解你的模式的方式,购买似乎不仅仅是一个连接表,它本身就是一个实体。因此,创建一个 Purchase 实体并与关联实体创建适当的关系。一个典型的连接表至少有两个指向相关表的外键。它可能具有代理主键或复合主键(使用两个外键的组合创建)。由于购买有一个主键(很可能是代理)和另一列是外键,因此更好地将购买建模为一个实体

于 2013-10-30T11:56:04.747 回答