I have some entities like this (a post
with some tags
associated with):
@Entity
public class Post {
@GeneratedId
public Long id;
@ManyToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
public Set<Tag> tags = new HashSet<Tag>();
...
}
@Entity
public class Tag {
@GeneratedId
public Long id;
public String name;
}
I have to make some requests that returns hundreds of Posts with filtering on tags.
Up to now, I have used standard queries returning some Post entities, then accessing the myPost.tags to create my POJO.
This is really time consuming because each time I create a POJO, I call myPost.tags which makes a request to get them. I thought it was a good idea to use the select new
feature like this:
select distinct new PostDTO(p.id, t) from Post p left outer join p.tags as t where ...
with
public class PostDTO {
public PostDTO(Long id, Tag[] tags) {...}
}
Unfortunately, due to JPQL: Receiving a Collection in a Constructor Expression the tags could not be passed as an array.
So my question: how can I do? Do I have to change my schema? or JPA has a feature that I've missed?