0

プロジェクトでかなり複雑な基準クエリを実行しようとしています。

この特定の例は、ORDER と PAYMENT の 2 つのデータベース テーブルで構成されています。ORDER テーブルには、注文の合計価格を含むフィールド (つまり、プロパティ「金額」と通貨の列挙値で構成される「Money」と呼ばれる組み込みの Hibernate エンティティ) があります。

PAYMENT テーブルには、顧客が支払った金額を含む金額列もあります。各注文には、注文の合計金額と支払いが含まれます。注文には支払いが必要ないため、支払いプロパティは null にすることができます。休止状態のマッピング自体は機能します。

私のDAOには、すでにいくつかの制限が含まれているOrderオブジェクトオブジェクトの基準があります。ここで行う必要があるのは、合計注文価格が 2 つの値の間にあるすべての注文を照会することです。これは次のようになります。

criteria.add(Restrictions.and(Restrictions.ge("orderTotalPrice.amount", amountFrom),
                    Restrictions.le("orderTotalPrice.amount", amountTo)));

さらに、支払い金額が同じ 2 つの値の間の支払いを含むすべての注文を取得する必要があります。paymentAmount が Order エンティティのプロパティである場合、次のようにすることができます。

criteria.add(Restrictions.or(
                    Restrictions.and(Restrictions.ge("orderTotalPrice.amount", amountTo),
                        Restrictions.le("orderTotalPrice.amount", amountFrom)),
                    Restrictions.and(Restrictions.ge("paymentAmount.amount", amountFrom),
                        Restrictions.le("paymentAmount.amount", amountTo))));

私の問題は、支払い金額が注文エンティティ内の支払いオブジェクト内にのみ存在することです。したがって、注文の支払い額を取得するには、「payment.paymentAmount.amount」のようなものが必要です。もちろんこれは機能しないので、通常は次のようなサブ基準を作成します。

 criteria.createCriteria("payment").add(
                    Restrictions.and(Restrictions.ge("paymentAmount.amount", amountFrom),
                        Restrictions.le("paymentAmount.amount", amountTo)));

上記の例と最初の例を基準に追加すると、注文価格と支払い金額の金額が同じでなければならないことを意味します。私が必要とするのは、これら 2 つの制限の間の「OR」です。

私の質問は次のとおりです。別の基準とサブ基準の間の OR を表す基準オブジェクトに基準を追加することは、基準 API によってサポートされていますか?

私の問題が何であるかが明確であることを願っています。さらに詳しい情報が必要な場合は、遠慮なくコメントを追加してください。

前もって感謝します!

PS: これらは (簡略化された) 休止状態のエンティティ クラスです。

@Entity
public class Order {

    @Embedded
    private Money orderTotalPrice;

    @OneToOne(optional = true)
    private Payment payment;

    // Getters & Setters
}


@Entity
public class Payment {

    @Embedded
    private Money paymentAmount;

    // Getters & Setters
}


@Embeddable
public class Money {
    @Column
    private BigDecimal amount;

    // Getters & Setters
}
4

1 に答える 1

0

必要なものを完全に理解しているかどうかはわかりませんが、サブ基準を使用するよりもエイリアスを使用する方が簡単なことがよくあります。

Criteria c = session.createCriteria(Order.class, "order");
// join with payment:
c.createAlias("order.payment", "p");
// now you can refer to properties of order.payment using the "p" alias
c.add(Restrictions.or(
    Restrictions.and(Restrictions.ge("p.paymentAmount.amount", amountFrom),
                     Restrictions.le("p.paymentAmount.amount", amountTo)),
    Restrictions.and(Restrictions.ge("order.orderTotalPrice.amount", amountTo),
                     Restrictions.le("order.orderTotalPrice.amount", amountFrom))));

これは、次の HQL をほぼそのまま翻訳したものなので、私にとってはより自然です。

select order from Order order
join order.payment p
where (p.paymentAmount.amount >= :amountFrom and p.paymentAmount.amount <= :amountTo)
or (order.orderTotalPrice.amount >= amountFrom and order.orderTotalPrice.amount <= amountTo)

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#querycriteria-associationsを参照してください。

于 2013-08-18T21:19:09.747 に答える