I'm trying to make a jpa query, but I'm getting an exception:
The state field "n.id" path cannot be resolved to a valid type.
My query is:
select distinct n from News n
left join n.commentsList as c
left join n.tagSet as nt
left join n.author as a
group by n.id, n.title, n.shortText, n.fullText, n.creationDate,
n.modificationDate, n.author.authorId, n.version
order by n.modificationDate desc, count(c.news) desc
My entity:
@Entity
@Table(name = "News")
public final class News implements Serializable, IEntity {
/**
* For deserialization with no exception after modification.
*/
private static final long serialVersionUID = 3773281197317274020L;
@Id
@SequenceGenerator(name = "NEWS_SEQ_GEN", sequenceName = "NEWS_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "NEWS_SEQ_GEN")
@Column(name = "NEWS_ID", precision = 0)
private Long newsId; // Primary key
@Column(name = "TITLE")
private String title;
@Column(name = "SHORT_TEXT")
private String shortText;
@Column(name = "FULL_TEXT")
private String fullText;
@Temporal(TemporalType.DATE)
@Column(name = "CREATION_DATE")
private Date creationDate;
@Temporal(TemporalType.DATE)
@Column(name = "MODIFICATION_DATE")
private Date modificationDate;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "news")
private List<Comment> commentsList;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "NEWS_TAG", joinColumns = { @JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { @JoinColumn(name = "TAG_ID") })
private Set<Tag> tagSet;
@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "NEWS_AUTHOR", joinColumns = { @JoinColumn(name = "NEWS_ID") }, inverseJoinColumns = { @JoinColumn(name = "AUTHOR_ID") })
private Author author;
@Version
private Long version;
I think that there is something wrong in alias and in the group by clause.
Your id is called
private Long newsId; // Primary key
Therefore that is the value you should use in your query. In other words:
group by n.newsId, ...
and not n.Id
I believe you need to adjust your query and just use the n.newsId
in your group by
clause.
select n, count(c) from News n
left join n.commentsList as c
left join n.tagSet as nt
left join n.author as a
group by n.id
order by n.modificationDate desc, count(c) desc
I think this will ensure that you can get the counts of the comments (though I'm not certain).