JPQL Group By Distinct

时间:2015-11-22 13:38:49

标签: java hibernate jpa jpql

假设我有以下实体(省略了注释/ getter设置器):

@Entity
public class Topic {

   @Id
   private Long id;
      private String title;
   private String text;

   @OneToMany(mappedBy = "topic", cascade = CascadeType.REMOVE)
   private List<Post> posts = new LinkedList();

}

@Entity
public class Post {

    @Id
    private Long id;
    private String text;

    @Temporal(TemporalType.DATE)
    private Date createdOn;

    @Temporal(TemporalType.DATE)
    private Date updatedOn;

    @ManyToOne
    private Topic topic;
}

我想运行JPQL查询,该查询基本上根据最新的TopicsPost进行排序。

所以我创建的查询如下:

select t from Topic t join t.posts p
group by t, p
order by p.updatedOn desc

然而这里的问题是我有重复的主题实体。甚至没有使用select distinct

有什么想法吗?

编辑:

SELECT
  topic0_.id               AS id1_13_,
  topic0_.text             AS text_r5_13_,
  topic0_.title            AS title7_13_
FROM topic topic0_ LEFT OUTER JOIN post posts1_
    ON topic0_.id = posts1_.topic_id

GROUP BY topic0_.id, posts1_.id
ORDER BY posts1_.updated_on DESC

0 个答案:

没有答案