1
我需要做一個與標準生成器的加入,然後得到Tuple
(Object[]
)類型的結果。第一個元組應該是Preke
(並且這個工作正常),第二個Tuple
元素應該是包含@OneToMany
對象的List<PrekeTiekejas>
。以下代碼代替List<PrekeTiekejas>
返回單個PrekeTiekejas
對象。我做錯了什麼?JPA criteriaquery元組onetomany加入
CriteriaBuilder cb = MinutisManager.getInstance().getCriteriaBuilder();
CriteriaQuery<Tuple> criteriaQuery = cb.createTupleQuery();
Root<minutis.Preke> from = criteriaQuery.from(minutis.Preke.class);
ListJoin<minutis.Preke, minutis.PrekeTiekejas> tiekejai = from.joinList(Preke_.tiekejai.getName());
criteriaQuery.multiselect(from, tiekejai);
TypedQuery<Tuple> typedQuery = MinutisManager.getInstance().createQuery(criteriaQuery);
//pages
typedQuery.setFirstResult(0);
typedQuery.setMaxResults(100);
//typedQuery.setHint(QueryHints.REFRESH, HintValues.TRUE);
List<Tuple> tuples = typedQuery.getResultList();
for(Tuple t : tuples) {
minutis.Preke preke = t.get(0, minutis.Preke.class);
System.out.println(preke.getPavadinimas());
List<minutis.PrekeTiekejas> tiek = t.get(1, List.class);
System.out.println(tiek.size());
}
以下是錯誤我得到:
Testcase: testGetPrekesByFilters(database.dao.PrekeDAOTest): Caused an ERROR
Element 1 type interface java.util.List is invalid for result "***PrekeTiekejas.toString()***".
java.lang.IllegalArgumentException: Element 1 type interface java.util.List is invalid for result "***PrekeTiekejas.toString()***".
at org.eclipse.persistence.internal.jpa.querydef.TupleImpl.get(TupleImpl.java:89)
at database.dao.PrekeDAOTest.testGetPrekesByFilters(PrekeDAOTest.java:69)
PrekeDAOTest.java:69
行:
List<minutis.PrekeTiekejas> tiek = t.get(1, List.class);