1

我有两节课

@Entity
@Table(name="AFFILIATES")
class Affiliate implements Serializable {

    @Id
    @Column(name= "affiliate_id")
    private String affiliateId;

    @OneToMany(mappedBy="affiliate", fetch=FetchType.LAZY)
    private Set<AffiliateInvoice> affiliateInvoice;

    @OneToMany(mappedBy="affiliate", fetch=FetchType.LAZY)
    private Set<Payments> payments;

    @Column(name="customer_id")
    private Integer customerId;

    public String getAffiliateId() {
        return affiliateId;
    }

    public void setAffiliateId(String affiliateId) {
        this.affiliateId = affiliateId;
    }

    public Set<AffiliateInvoice> getAffiliateInvoice() {
        return affiliateInvoice;
    }

    public void setAffiliateInvoice(Set<AffiliateInvoice> affiliateInvoice) {
        this.affiliateInvoice = affiliateInvoice;
    }

    public Set<Payments> getPayments() {
        return payments;
    }

    public void setPayments(Set<Payments> payments) {
        this.payments = payments;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

}

@Entity
@Table(name="AFFILIATE_INVOICE")
class AffiliateInvoice implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="invoice_id")
    private Integer invoiceId;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="affiliate_id")
    private Affiliate affiliate;

    @OneToOne
    @JoinColumn(name="payment_id")
    private Payments payments;

    public Integer getInvoiceId() {
        return invoiceId;
    }

    public void setInvoiceId(Integer invoiceId) {
        this.invoiceId = invoiceId;
    }

    public Affiliate getAffiliate() {
        return affiliate;
    }

    public void setAffiliate(Affiliate affiliate) {
        this.affiliate = affiliate;
    }

    public Payments getPayments() {
        return payments;
    }

    public void setPayments(Payments payments) {
        this.payments = payments;
    }

}

@Entity
@Table(name= "PAYMENTS")
class Payments implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="payment_id")
    private Integer paymentId;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="merchant_id", referencedColumnName="customer_id")
    private Affiliate affiliate;

    @OneToOne
    @JoinColumn(name="payment_id", nullable=true)
    private AffiliateInvoice affiliateInvoice;

    @Column(name="amount")
    private Double amount;

    public Integer getPaymentId() {
        return paymentId;
    }

    public void setPaymentId(Integer paymentId) {
        this.paymentId = paymentId;
    }

    public Affiliate getAffiliate() {
        return affiliate;
    }

    public void setAffiliate(Affiliate affiliate) {
        this.affiliate = affiliate;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public AffiliateInvoice getAffiliateInvoice() {
        return affiliateInvoice;
    }

    public void setAffiliateInvoice(AffiliateInvoice affiliateInvoice) {
        this.affiliateInvoice = affiliateInvoice;
    }
}

现在我可能有机会,附属公司可能有也可能没有发票。

现在 Affiliate 有 Payments,但一些会员可能还没有生成发票。

那么我如何才能检索有多少会员生成发票以及有多少待处理的所有详细信息?

我做了如下的事情。

Session s = connection.getSession();
            String hql = "from Affiliate as a " +
                    " inner join fetch a.payments as mp " +
                    " inner join fetch a.affiliateInvoice as ai " +
                    " where ai.invoiceId = 270";
                    Query q = s.createQuery(hql);

                    AffiliateInvoice ai = (AffiliateInvoice) q.uniqueResult();

                    System.out.println(ai);

但它没有给出结果

我想要类似的东西。

AffiliateId PaymentId isInvoiceGenerated
aaa 111 是
bbb 222 牛
ccc 333 N
ddd 444 是

我怎样才能得到这样的结果?

如果我做如下的事情

Session s = connection.getSession();
            String hql = "from com.infibeam.test.oneToMany.Affiliate as a " +
                    " inner join fetch a.payments as mp " +
                    " left join fetch a.affiliateInvoice as ai "

然后它给了我

java.lang.ClassCastException: Affiliate cannot be cast to java.lang.String
at org.hibernate.type.StringType.toString(StringType.java:44)
    at org.hibernate.type.NullableType.toLoggableString(NullableType.java:168)
    at org.hibernate.pretty.MessageHelper.collectionInfoString(MessageHelper.java:284)
4

0 回答 0