我正在尝试进行jpa查询,但我得到了一个例外:
状态字段“n.id”路径无法解析为有效类型。
我的查询是:
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
我的实体:
@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;
我认为别名和group by子句中存在错误。
答案 0 :(得分:0)
您的ID被称为
private Long newsId; // Primary key
因此,这是您应在查询中使用的值。换句话说:
group by n.newsId, ...
而不是n.Id
我认为您需要调整查询,并在n.newsId
子句中使用group by
。
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
我认为这可以确保您可以获得评论的计数(虽然我不确定)。