繁体   English   中英

Spring Boot JPA和Criteria API-选择单列

[英]Spring boot JPA & Criteria API - Select single column

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

SQL

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

我有Comment表和Note表。 注释表具有comment, note and version列。 评论本身就是一个注释。 现在,我想检索版本大于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;
}

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

错误 return语句出错,

Cannot cast from List<Comment> to List<Note>

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

cb.createQuery参数在您可以看到的文档中接受result类。

更新

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

您的条件查询的根是“ CommentNote

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>

不需要编写自定义存储库方法,因为您正在创建的存储库方法已经在spring-data中生成。

如果您的存储库扩展了CrudRepository,您将免费获得该方法。

模式为findAllBy [propertyOfClass]。

但是请注意,您的实体中实际上没有任何NOTE的集合。

也许您应该首先将OneToOne关联更改为OneToMany。

可以构建为条件查询,如下所示:

CriteriaQuery<Country> q = cb.createQuery(Country.class);

Root<Country> c = q.from(Country.class);

q.select(c.get("currency")).distinct(true);

select方法采用一个类型为Selection的参数并将其设置为SELECT子句的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM