1
我遇到問題,我找不出哪個代碼存在問題。 我是新日本Jpa項目,我無法弄清楚。 在此先感謝!Java JPA代碼中的Mysql錯誤:狀態字段路徑無法解析爲有效類型
這是我得到的錯誤。它更長,但我希望這已經足夠了。
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [SELECT item FROM NewsArticle item WHERE item.user.idUser = '0'].
[40, 56] The state field path 'item.user.idUser' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1743)
有問題的行:
Query getFeed = entityManager.createQuery("SELECT item FROM NewsArticle item WHERE item.user.idUser = \'" + loggedUser.getIduser() + "\'");
並與生成的類生成實體從表:
/**
* The persistent class for the news_articles database table.
*
*/
@Entity
@Table(name="news_articles")
@NamedQuery(name="NewsArticle.findAll", query="SELECT n FROM NewsArticle n")
public class NewsArticle implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int idnews;
@Temporal(TemporalType.TIMESTAMP)
private Date date;
private String description;
private String source;
//bi-directional many-to-one association to Comment
@OneToMany(mappedBy="newsArticle")
private List<Comment> comments;
//bi-directional many-to-many association to Category
@ManyToMany
@JoinTable(
name="news_category"
, joinColumns={
@JoinColumn(name="idNews_nc")
}
, inverseJoinColumns={
@JoinColumn(name="idCategory_nc")
}
)
private List<Category> categories;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="userID")
private User user;
public NewsArticle() {
}
public int getIdnews() {
return this.idnews;
}
public void setIdnews(int idnews) {
this.idnews = idnews;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSource() {
return this.source;
}
public void setSource(String source) {
this.source = source;
}
public List<Comment> getComments() {
return this.comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
public Comment addComment(Comment comment) {
getComments().add(comment);
comment.setNewsArticle(this);
return comment;
}
public Comment removeComment(Comment comment) {
getComments().remove(comment);
comment.setNewsArticle(null);
return comment;
}
public List<Category> getCategories() {
return this.categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
「item.user」e因爲你在班上展示它。您不會顯示「用戶」類,因此人們不知道該字段(「idUser」)是否存在於該類中。您也可以嘗試爲用戶進行EXPLICIT加入,以更好地控制加入 –