I'm trying to make a jpa query, but I'm getting an exception:
我正在尝试做一个jpa查询,但是我得到了一个例外:
The state field "n.id" path cannot be resolved to a valid type.
状态字段“n。无法将id“路径解析为有效类型。
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.
我认为别名和group by子句有问题。
1 个解决方案
#1
0
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
而不是n.Id
I believe you need to adjust your query and just use the n.newsId
in your group by
clause.
我相信您需要调整您的查询并使用n。按子句在你的组里。
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).
我认为这将确保你能得到评论的数量(尽管我不确定)。
#1
0
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
而不是n.Id
I believe you need to adjust your query and just use the n.newsId
in your group by
clause.
我相信您需要调整您的查询并使用n。按子句在你的组里。
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).
我认为这将确保你能得到评论的数量(尽管我不确定)。