JPQL Group Distinctive Feature

Suppose I have the following entities (annotations / getters seters omitted):

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

I want to run a query JPQLthat basically sorts Topicsaccording to the latter Post.

So, the query I created looks like this:

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

However, the problem is that I have duplicate theme objects . Doesn't even work select distinct.

Any ideas?

Edit:

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
+4
source share

Source: https://habr.com/ru/post/1616941/


All Articles