JPA。 JOIN嵌套SELECT"意外令牌("

时间:2015-06-08 09:24:10

标签: java jpa jpql

我有新闻实体,想要获得按评论数量排序的新闻列表。 我试着这样做:

Query query = manager.createQuery("SELECT n FROM News n LEFT JOIN \n" +
                "(SELECT c.newsId, COUNT(c.newsId) as commentCount FROM COMMENTS c GROUP BY c.newsId) c ON n.newsId=c.newsId" +
                "ORDER BY c.commentCount DESC NULLS LAST, n.modificationDate DESC");

但我有"意外的令牌:(" LEFT JOIN之后。我试着写不同的查询,但没有成功。请帮我解决这个问题。

News.java:

    @Entity
    @Table(name = "NEWS")
    public class News implements Serializable{
        /**
         * serialVersionUID
         */
        private static final long serialVersionUID = 883279937885116359L;
        /**
         * News id
         */
        @Id
        @GeneratedValue(generator = "seq")
        @SequenceGenerator(name="seq", sequenceName="NEWS_SEQ",allocationSize=1)
        @Column(name = "NEWS_ID", nullable = false, unique = true)
        private Long id;
        /**
         * News short text
         */
        @Column(name = "SHORT_TEXT")
        private String shortText;
        /**
         * News full text
         */
        @Column(name = "FULL_TEXT")
        private String fullText;
        /**
         * News title
         */
        @Column(name = "TITLE")
        private String title;
        /**
         * News creation date
         */
        @Column(name = "CREATION_DATE")
        private Date creationDate;
        /**
         * News modification date
         */
        @Column(name = "MODIFICATION_DATE")
        private Date modificationDate;
        /**
         * News author
         */
        @ManyToOne(cascade = CascadeType.ALL)
        @JoinTable(
                name = "NEWS_AUTHOR",
                joinColumns = {@JoinColumn(name = "NEWS_ID")},
                inverseJoinColumns =  {@JoinColumn(name = "AUTHOR_ID")}
        )
        private Author author;
        /**
         * News tags
         */
        @OneToMany(cascade = CascadeType.ALL)
        @JoinTable(
                name = "NEWS_TAG",
                joinColumns = {@JoinColumn(name = "NEWS_ID")},
                inverseJoinColumns =  {@JoinColumn(name = "TAG_ID")}
        )
        private List<Tag> tags;
        /**
         * News comments
         */
        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "NEWS_ID")
        private List<Comment> comments;
...

Comment.java:

    @Entity
    @Table(name = "COMMENTS")
    public class Comment implements Serializable{
        /**
         * serialVersionUID
         */
        private static final long serialVersionUID = -5697896094322498108L;
        /**
         * Comment id
         */
        @Id
        @GeneratedValue(generator = "seq")
        @SequenceGenerator(name="seq", sequenceName="COMMENTS_SEQ",allocationSize=1)
        @Column(name = "COMMENT_ID", nullable = false, unique = true)
        private Long id;
        /**
         * Comment text
         */
        @Column(name = "COMMENT_TEXT")
        private String commentText;
        /**
         * Comment creation date
         */
        @Column(name = "CREATION_DATE")
        private Date creationDate;
        /**
         * Id of the news which the comment is added to
         */
        @Column(name = "NEWS_ID")
        private Long newsId;
...

1 个答案:

答案 0 :(得分:1)

如果您只想按评论数量订购新闻,则应该这样做

select n from News n order by size(n.comments)