本文介绍了春季启动JPA&条件API-选择单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从表中检索单个列,但遇到有关返回类型的编译错误。

I am trying to retrieve a single column from the table, but I am getting compilation error about return type.

SQL

select oComment from comment where oNote = note and version > 0;

我有 Comment 表和 Note 表。评论表中有条评论,注释和版本列。评论本身就是一个注释。现在,我想检索版本大于0的注释的所有注释。但是在这里,我只想注释类型为注释的注释列。

I have Comment table and Note table. Comment table has comment, note and version columns. The comment itself is a note. Now I want to retrieve all comments of the note which has version greater than 0. But here I want only comment column which of note type.

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;
}

CustomRepositoryMethod

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

        Subquery<Note> subquery = cq.subquery(Note.class);
        Root<Note> note = subquery.from(Note.class);

        cb.desc(note.get("m_dtCreationDate"));

        cq.where(predicates.toArray(new Predicate[0]));

        cq.multiselect(oComment.<Note>get("oComment"));

        return (List<Note>)em.createQuery(cq).setFirstResult(iOffset).setMaxResults(iResultSize).getResultList();
    }

错误
返回语句错误,

errorError at return statement,

无法从List< Comment>列出< Note>

推荐答案

CustomRepositoryMethod 中替换第一个
CriteriaQuery< Comment> cq = cb.createQuery(Comment.class); CriteriaQuery< Note> cq = cb.createQuery(Note.class)

cb.createQuery 参数接受。

更新

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

这篇关于春季启动JPA&amp;条件API-选择单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:25