gpt4 book ai didi

java - 状态字段路径无法解析为有效类型。 JPA查询

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:38 26 4
gpt4 key购买 nike

我正在尝试进行 jpa 查询,但遇到异常:

The state field "n.id" path cannot be resolved to a valid type.

我的查询是:

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;

我认为alias和group by子句有问题。

最佳答案

您的 ID 已被调用

private Long newsId; // Primary key

因此,这是您应该在查询中使用的值。换句话说:

group by n.newsId, ...

而不是n.Id

我认为您需要调整查询,只需在 group by 子句中使用 n.newsId 即可。

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

我认为这将确保您可以获得评论的数量(尽管我不确定)。

关于java - 状态字段路径无法解析为有效类型。 JPA查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com