4 回答

TA貢獻1851條經(jīng)驗 獲得超5個贊
在CustomRepositoryMethod中將第一行替換CriteriaQuery<Comment> cq = cb.createQuery(Comment.class);為CriteriaQuery<Note> cq = cb.createQuery(Note.class)
cb.createQuery參數(shù)在您可以看到的文檔中接受結(jié)果類。
更新
// assuming query like
// select oComment from comment inner join Note on comment.noteuuid=Note.noteuuid where Note.noteUuid = 1 and version > 0;
CriteriaBuilder cb = em.getCriteriaBuilder();
// data type of oComment
CriteriaQuery<Note> query = cb.createQuery(Note.class);
// from comment
Root<Comment> comment = query.from(Comment.class);
//join
Join<Comment, Note> note = comment.join(comment.get("oNote"));
//version Condition
Predicate version=cb.greaterThan(comment.get("version"),0 );
//Note condition
predicate note=cb.equal(note.get("noteuuid"),note.getNoteUuid());
// get oComment and where condtion
query.select(comment.get("oComment")).where(cb.and(version,note));
return em.createQuery(query).setFirstResult(iOffset).setMaxResults(iResultSize).getResultList();

TA貢獻1829條經(jīng)驗 獲得超7個贊
您的條件查詢的根Comment不是Note
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Comment> cq = cb.createQuery(Comment.class);
Root<Comment> oComment = cq.from(Comment.class);
你正在嘗試做
return (List<Note>)em.createQuery(cq).setFirstResult(iOffset)
.setMaxResults(iResultSize).getResultList();
在這種情況下編譯錯誤是不可避免的,因為不會em.createQuery(cq).getResultList()返回List<Comment>List<Note>

TA貢獻1836條經(jīng)驗 獲得超4個贊
沒有必要編寫自定義存儲庫方法,因為您正在創(chuàng)建的存儲庫方法已經(jīng)在 spring-data 中生成。
如果您的存儲庫擴展了 CrudRepository,您將免費獲得您正在尋找的方法。
模式是 findAllBy[propertyOfClass]。
但請注意,您的實體中實際上沒有 Collection of NOTE。
也許您應(yīng)該首先將 OneToOne 關(guān)聯(lián)更改為 OneToMany。

TA貢獻1796條經(jīng)驗 獲得超4個贊
可以構(gòu)建為條件查詢,如下所示:
CriteriaQuery<Country> q = cb.createQuery(Country.class);
Root<Country> c = q.from(Country.class);
q.select(c.get("currency")).distinct(true);
該select方法采用一個 Selection 類型的參數(shù)并將其設(shè)置為 SELECT 子句內(nèi)容。
添加回答
舉報