2017-08-27 30 views
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; 
    } 

} 
+0

「item.user」e因爲你在班上展示它。您不會顯示「用戶」類,因此人們不知道該字段(「idUser」)是否存在於該類中。您也可以嘗試爲用戶進行EXPLICIT加入,以更好地控制加入 –

回答

1

您定義idint

@Id 
private int idnews; 

然後在查詢中你把它放在兩個引號之間?

item.user.idUser = \'" + loggedUser.getIduser() + "\'" 
//------------------^-------------------------------^ 

這是什麼使這個問題,所以解決它只是刪除引號


重要

它是不安全的查詢避免concatination旁邊,而不是你能使用setParameter例如:

Query getFeed = entityManager.createQuery("SELECT item FROM NewsArticle item WHERE item.user.idUser = :id"); 
getFeer.setParameter("id", loggedUser.getIduser()); 
相關問題