I have entity objects:
@Entity
public class Tag {
@Id
private Long id;
@Transient
private int count;
// getter, setter etc..
}
@Entity
public class Request {
// fileds
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Tag> tag = new HashSet<Tag>();
// getter, setter etc..
}
I need to get all tags with count by Request.
In DAO I make function for it with SQL query:
select tag as id, count(rt.request) as count
from request_tag rt
where rt.request in (...) and rt.request in (...) and etc...
group by rt.tag order by count desc
Tags found but count isn’t binding.
How can I bind count from query?
PS:
-
I don’t want to remove @Transient annotation (because I don’t want keep count in DB).
-
I don’t want to use @Formula (because it will be slow).
Option 1:
Create named query with
select tag as id, count(rt.request) as count, after execution you will getObject[2]cast as expected and use.Option 2:
You can create yet another entity (saying TagStatistic) without @Transient
and map it to native(!) named query