我想在一個項目中做一個相當複雜的標準查詢。或限制,包括一個子標準
這個特殊的例子包含2個數據庫表:ORDER和PAYMENT。 ORDER表具有一個字段(即一個名爲「Money」的嵌入式Hibernate實體,包含一個屬性「金額」和貨幣的枚舉值),其中包含訂單的總價格。
付款表中還有一個金額列,其中包含客戶已支付的金額。 每個訂單包含訂單總價和付款。訂單不必付款,所以付款屬性可以爲空。 hibernate映射本身起作用。
在我的DAO中,我有一個Order對象對象的Criteria已經包含了一些限制。我現在需要做的是查詢所有訂單價格在2個值之間的訂單。這看起來像這樣:
criteria.add(Restrictions.and(Restrictions.ge("orderTotalPrice.amount", amountFrom),
Restrictions.le("orderTotalPrice.amount", amountTo)));
此外,我需要獲得所有訂單包含一個付款金額在相同的兩個值之間的付款。如果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)));
添加上面的例子與第一例子至標準意味着該數額必須是訂單的同價格和付款金額。我需要的是這兩個限制之間的「或」。
所以我的問題是: Criteria API是否支持添加Criterion到條件對象來表示另一個Criterion和子條件之間的OR?
我希望清楚我的問題是什麼。如果有人需要更多信息,請不要猶豫,以添加評論!
在此先感謝!
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
}