Java 弹簧靴JPA&;标准API-选择单列

Java 弹簧靴JPA&;标准API-选择单列,java,hibernate,spring-boot,jpa-2.0,criteria-api,Java,Hibernate,Spring Boot,Jpa 2.0,Criteria Api,我试图从表中检索一个列,但得到了有关返回类型的编译错误 SQL select oComment from comment where oNote = note and version > 0; 我有注释表和注释表。注释表有注释、注释和版本列。评论本身就是一个注释。现在我想检索版本大于0的注释的所有注释。但这里我只想要注释类型的注释列 Comment.java @Entity @Table(name="comment") @Cache(usage=CacheConc

我试图从表中检索一个列,但得到了有关返回类型的编译错误

SQL

select oComment from comment where oNote = note and version > 0;
我有
注释
表和
注释
表。注释表有
注释、注释和版本列。评论本身就是一个注释。现在我想检索版本大于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;
}
@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;
}
Note.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;
}
@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();
    }
public List findAllByNote(注意oNote、int-iOffset、int-iResultSize){
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Comment.class);
根oComment=cq.from(Comment.class);
列表谓词=新的ArrayList();
add(cb.equal(oComment.get(“oNote”),oNote));
add(cb.greaterThan(oComment.get(“version”),0));
Subquery Subquery=cq.Subquery(Note.class);
Root note=subquery.from(note.class);
cb.desc(注:get(“m_dtCreationDate”);
cq.where(谓词toArray(新谓词[0]);
cq.multiselect(oComment.get(“oComment”);
return(List)em.createQuery(cq).setFirstResult(iOffset).setMaxResults(iResultSize).getResultList();
}
错误 返回语句出错


无法从一个列表转换到另一个列表

无需编写自定义存储库方法,因为您正在创建的存储库方法已在spring数据中生成

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

模式是findAllBy[propertyOfClass]

但请注意,您的实体中实际上没有票据集合


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

您的条件查询的根是
注释
而不是
注释

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Comment> cq = cb.createQuery(Comment.class);
Root<Comment> oComment = cq.from(Comment.class);
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Comment.class);
根oComment=cq.from(Comment.class);
而你正试图做到这一点

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

在这种情况下编译错误是不可避免的,因为
em.createQuery(cq).getResultList()
将首先在CustomRepositoryMethod中返回
列表
而不是
列表

CriteriaQuery cq=cb.createQuery(Comment.class)
to
CriteriaQuery 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();
//假设查询类似
//从注释上的注释内部连接注释中选择oComment。noteuuid=Note.noteuuid,其中Note.noteuuid=1且版本>0;
CriteriaBuilder cb=em.getCriteriaBuilder();
//oComment的数据类型
CriteriaQuery=cb.createQuery(Note.class);
//评论
根注释=query.from(comment.class);
//加入
joinnote=comment.Join(comment.get(“oNote”);
//版本条件
谓词version=cb.greaterThan(comment.get(“version”),0;
//注释条件
谓词note=cb.equal(note.get(“noteuid”),note.getnoteuid();
//获取oComment和何处条件
query.select(comment.get(“oComment”)).where(cb.和(version,note));
返回em.createQuery(query).setFirstResult(iOffset).setMaxResults(iResultSize).getResultList();

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

CriteriaQuery q=cb.createQuery(Country.class);
根c=q.from(国家级);
q、 选择(c.get(“货币”)).distinct(true);

select
方法接受类型选择的一个参数,并将其设置为select子句内容。

我需要注释表中的注释列以及注释和版本列的帮助。如果我按照您的建议更改了代码,则查询将应用于Notes表,并在运行时为我提供查询syntex。您能告诉我要执行哪个查询吗?我已经添加了查询,并且刚刚添加了简要说明。您的查询无效。oNote是实体而不是列。可以给我看一下你的笔记类吗?增加了笔记类。是,但它的注释表有noteid.Yes。我想从comment表中得到comment列,这就是我这么做的原因。