第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring boot JPA & Criteria API - 選擇單列

Spring boot JPA & Criteria API - 選擇單列

慕絲7291255 2023-02-23 17:24:44
我正在嘗試從表中檢索單個列,但出現(xiàn)有關(guān)返回類型的編譯錯誤。數(shù)據(jù)庫select oComment from comment where oNote = note and version > 0;我有Comment桌子和Note桌子。評論表有comment, note and version列。評論本身就是一個注釋?,F(xiàn)在我想檢索版本大于 0 的注釋的所有評論。但是在這里我只想要注釋類型的注釋列。Comment.java    @Entity    @Table(name="comment")    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="comments")    public class Comment implements Serializable {        private static final long serialVersionUID = -4420192568334549165L;        public Comment() {        }        @Id        @OneToOne        @JoinColumn(name="commentuuid",referencedColumnName="noteuuid")        private Note oComment;        @Id        @OneToOne        @JoinColumn(name="noteuuid",referencedColumnName="noteuuid")        private Note oNote;}Note.java@Entity@Table(name = "note")@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="notes")public class Note implements Serializable{    private static final long serialVersionUID = 4089174391962234433L;    @Column(name="title")    private String m_szTitle;    @Column(name="htmlcontent")    private String m_sbHtmlContent;    @Column(name="textcontent")    private String m_sbTextContent;    @Id    @Column(name="noteuuid", columnDefinition="varchar(36)")    private String noteUuid;}自定義存儲庫方法public List<Note> findAllByNote(Note oNote, int iOffset, int iResultSize) {        CriteriaBuilder cb = em.getCriteriaBuilder();        CriteriaQuery<Comment> cq = cb.createQuery(Comment.class);        Root<Comment> oComment = cq.from(Comment.class);        List<Predicate> predicates = new ArrayList<>();        predicates.add(cb.equal(oComment.get("oNote"), oNote));        predicates.add(cb.greaterThan(oComment.get("version"), 0));
查看完整描述

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();



查看完整回答
反對 回復(fù) 2023-02-23
?
千巷貓影

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>


查看完整回答
反對 回復(fù) 2023-02-23
?
HUH函數(shù)

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。


查看完整回答
反對 回復(fù) 2023-02-23
?
SMILET

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)容。


查看完整回答
反對 回復(fù) 2023-02-23
  • 4 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號