OneToMany和ManyToOne报告通过hibernate生成

时间:2013-10-30 05:53:53

标签: hibernate annotations reporting using

我有两个班级

@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;
    }
}

现在,联盟会员可能会或可能没有发票。

现在会员有付款,但有些会员可能还没有生成发票。

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

我做了如下事情。

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             Y
bbb              222             N
ccc              333             N
ddd              444             Y

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

如果我按照以下方式执行

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)

0 个答案:

没有答案