2011-09-13 41 views
3

我試圖使用包裝類作爲結果類型進行查詢。很抱歉,這篇較長的文章,但我想盡可能地完成我的問題。 根類有3只列出了我要檢索:帶有列表的JPA CriteriaQuery投影

@Entity 
@Table(name = "cash") 
public final class Cash extends BaseSimpleModel { 
    @Id 
    @SequenceGenerator(name = "id", sequenceName = "cash_seq") 
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "id") 
    private Long cashID; 

    @Column(length = 50, unique = true) 
    private String description; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) 
    @JoinColumn(name = "cashID") 
    private List<CashAllowedValue> allowedValueList; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) 
    @JoinColumn(name = "cashID") 
    private List<CashAllowedCurrency> allowedCurrencyList; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) 
    @JoinColumn(name = "cashID") 
    private List<CashAllowedCashier> allowedCashierList; 
    ... getters and setters 
} 

這裏是我的包裝類:

public class CashQueryResult extends QueryResult { 
    private Long cashID; 
    private String description; 
    private List<CashAllowedValue> allowedValueList; 
    private List<CashAllowedCurrency> allowedCurrencyList; 
    private List<CashAllowedCashier> allowedCashierList; 

    public CashQueryResult(Long id, String description, List<CashAllowedValue> allowedValueList, List<CashAllowedCurrency> allowedCurrencyList, List<CashAllowedCashier> allowedCashierList) 
    { 
     this.cashID = id; 
     this.description = description; 
     this.allowedValueList = allowedValueList; 
     this.allowedCurrencyList = allowedCurrencyList; 
     this.allowedCashierList = allowedCashierList; 
    } 
    ... getters 
} 

這裏是我的查詢:

public final List<CashQueryResult> getQRList(final CashQueryParameter cashQP, final QueryHint queryHint) { 
    List<CashQueryResult> cashQRL = null; 
    try { 
     final CriteriaBuilder cb = em.getCriteriaBuilder(); 
     final PredicateBuilder pb = new PredicateBuilder(cb); 
     final CriteriaQuery<CashQueryResult> cq = cb.createQuery(CashQueryResult.class); 
     final Root<Cash> cash = cq.from(getModelClass()); 

     // Joins. 
     final ListJoin<Cash, CashAllowedValue> allowedValueList = cash.join(Cash_.allowedValueList, JoinType.LEFT); 
     final ListJoin<Cash, CashAllowedCurrency> allowedCurrencyList = cash.join(Cash_.allowedCurrencyList, JoinType.LEFT); 
     final ListJoin<Cash, CashAllowedCashier> allowedCashierList = cash.join(Cash_.allowedCashierList, JoinType.LEFT); 

     // Paths. 
     final Path<ValueTypeEnum> valueType = allowedValueList.get(CashAllowedValue_.valueType); 
     final Path<Currency> currency = allowedCurrencyList.get(CashAllowedCurrency_.currency); 
     final Path<IntranetUser> intranetUser = allowedCashierList.get(CashAllowedCashier_.cashier); 

     // Expressions. Just for testing purposes. 
     final Expression<List<CashAllowedValue>> cashAllowedValueListExpression = cash.get(Cash_.allowedValueList); 
     final Expression<List<CashAllowedCurrency>> cashAllowedCurrencyExpression = cash.get(Cash_.allowedCurrencyList); 
     final Expression<List<CashAllowedCashier>> cashAllowedCashierExpression = cash.get(Cash_.allowedCashierList); 

     cq.distinct(true); 
     cq.select(cb.construct(CashQueryResult.class, cash.get(Cash_.cashID), cash.get(Cash_.description), 
      // cash.get(Cash_.allowedValueList), cash.get(Cash_.allowedCurrencyList), cash.get(Cash_.allowedCashierList) // does not work 
      // cashAllowedValueListExpression, cashAllowedCurrencyExpression, cashAllowedCashierExpression // does not work 
      allowedValueList, allowedCurrencyList, allowedCashierList // does not work 
     )); 

     // cq.multiselect(cash.get(Cash_.cashID), cash.get(Cash_.description), 
     //  allowedValueList, allowedCurrencyList, allowedCashierList 
     //); // does not work 

     cq.where(cb.and(pb.like(cash.get(Cash_.description), cashQP.getDescription()), pb.equal(valueType, cashQP.getValueType()), pb.equal(currency.get(Currency_.currencyID), cashQP.getCurrencyID()), pb.equal(intranetUser.get(IntranetUser_.agentID), cashQP.getIntranetUserID()))); 
     cq.orderBy(cb.asc(cash.get(Cash_.description))); 

     final TypedQuery<CashQueryResult> tq = em.createQuery(cq); 
     tq.setFirstResult(queryHint.getFirstResult()); 
     tq.setMaxResults(queryHint.getMaxResults()); 
     cashQRL = tq.getResultList(); 
    } 
    catch (Throwable t) { 
     throw new EJBException(t.getMessage()); 
    } 
    return cashQRL; 
} 

最後,異常(這取決於我嘗試的選擇方法,但總是這樣):

2011-09-12 16:12:26,165 ERROR [org.hibernate.hql.PARSER] (http-127.0.0.1-8080-2) Unable to locate appropriate constructor on class [com.ebizlink.adonis.erp.model.support.CashQueryResult] [cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.ebizlink.adonis.erp.model.support.CashQueryResult] 
2011-09-12 16:12:26,169 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-127.0.0.1-8080-2) javax.ejb.EJBException: org.hibernate.hql.ast.QuerySyntaxException: 
Unable to locate appropriate constructor on class [com.ebizlink.adonis.erp.model.support.CashQueryResult] 
[select distinct new com.ebizlink.adonis.erp.model.support.CashQueryResult(generatedAlias0.cashID, generatedAlias0.description, generatedAlias1, generatedAlias2, generatedAlias3) 
from com.ebizlink.adonis.erp.model.Cash as generatedAlias0 
    left join generatedAlias0.allowedValueList as generatedAlias1 
    left join generatedAlias0.allowedCurrencyList as generatedAlias2 
    left join generatedAlias0.allowedCashierList as generatedAlias3 
where (1=1) and (1=1) and (1=1) and (1=1) 
order by generatedAlias0.description asc] 

僅供參考,以下是PredicateBuilder以防萬一。 我搜索了網頁,但我找不到想要檢索多個列表的人。 我錯過了一些明顯的東西嗎?多次提取是一個不行(休眠錯誤),我也不能使列表渴望。

另一個相關的問題是: 我可以做一個查詢,以便我的包裝類的列表也是實體包裝而不是實體? (例如:代替使用Cashiers,只需要名字和姓氏,並使用包含兩個字符串的CashierWrapper列表)

我真的很感謝你,我希望你能幫助我。

回答

1

很可能是你不能做,什麼規格說,有關參數表達式構造:

constructor_expression :: = NEW constructor_name(constructor_item {,constructor_item} *) constructor_item :: = single_valued_pa​​th_expression |標量表達式|聚合表達式| identification_variable

您試圖提供給構造函數的列表(collection_valued_field)不是constructor_item中的任何一個。

+0

這是真的。謝謝!我會看看我能做些什麼。 – Eus777