JPQL: The state field path cannot be resolved to a

2019-03-27 17:05发布

I can't make this query work:

Query query = eManager.createQuery("select c FROM News c WHERE c.NEWSID = :id",News.class);
        return (News)query.setParameter("id", newsId).getSingleResult();

and I got this exception:

Exception Description: Problem compiling [select c FROM News c WHERE c.NEWSID = :id]. 
[27, 35] The state field path 'c.NEWSID' cannot be resolved to a valid type.] with root cause
Local Exception Stack: 
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [select c FROM News c WHERE c.NEWSID = :id]. 

Why does it happen? :id and named parameter are identical

EDIT: my entity class

@Entity
@Table(name="NEWS")
public class News implements Serializable{

    @Id 
    @SequenceGenerator(name = "news_seq_gen", sequenceName = "news_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "news_seq_gen")
    private int newsId;
    private String newsTitle;
    private String newsBrief;
    private String newsContent;
    private Date newsDate;
    @Transient
    private boolean selected=false;

//constructor and getters and setters 

标签: jpql
3条回答
Explosion°爆炸
2楼-- · 2019-03-27 17:44

My entity is:

@Entity
@Table(name = "TBL_PERSON_INFO")
public class Person implements Serializable {
    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    @Column(name = "USER_ID", nullable = false)
    private Integer user_id;
    .
    .
    .
}

my query is (JPQL):

String queryName = "from Person p where p.user_id = :user_id";

so I use it like this:

        javax.persistence.Query query = em.createQuery(queryName);
        query.setParameter("user_id", userId);

        try {
            obj = query.getSingleResult();
        }
        catch (javax.persistence.NoResultException nre) {
            logger.error("javax.persistence.NoResultException: " + nre.getMessage());
        }
        catch (javax.persistence.NonUniqueResultException nure) {
            logger.error("javax.persistence.NonUniqueResultException: " + nure.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        if (obj == null) {
            System.out.println("obj is null!");
            return null;
        }

        Person person = (Person) obj;

It's work ;-)

查看更多
再贱就再见
3楼-- · 2019-03-27 17:45

That happens because News entity does not have persistent attribute named NEWSID. Names of the persistent attributes are case sensitive in JPQL queries and those should be written with exactly same case as they appear in entity.

Because entity have persistent attribute named newsId, that should also be used in query instead of NEWSID:

select c FROM News c WHERE c.newsId = :id
查看更多
对你真心纯属浪费
4楼-- · 2019-03-27 17:56

entity have persistent attribute named newsId.but in query you have used NEWSID . try with this

select c FROM News c WHERE c.newsId = :id
查看更多
登录 后发表回答