【问题标题】:JPQL: The state field path cannot be resolved to a valid typeJPQL:状态字段路径无法解析为有效类型
【发布时间】:2013-12-02 03:18:48
【问题描述】:

我无法使这个查询工作:

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

我得到了这个例外:

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]. 

为什么会这样? :id 和命名参数是相同的

编辑: 我的实体类

@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


    【解决方案1】:

    entity 具有名为 newsId.but 的持久属性,但在查询中您使用了 NEWSID 。试试这个

    select c FROM News c WHERE c.newsId = :id
    

    【讨论】:

      【解决方案2】:

      我的实体是:

      @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;
          .
          .
          .
      }
      

      我的查询是 (JPQL):

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

      所以我这样使用它:

              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;
      

      它的工作;-)

      【讨论】:

        【解决方案3】:

        这是因为News 实体没有名为NEWSID 的持久属性。持久属性的名称在 JPQL 查询中区分大小写,并且应该使用与它们在实体中出现的完全相同的大小写。

        由于实体具有名为newsId 的持久属性,因此也应该在查询中使用该属性而不是NEWSID

        select c FROM News c WHERE c.newsId = :id
        

        【讨论】:

        • hm 但实际上确实如此:这是我的实体:
        • 参数名称不是问题 - 持久属性的名称是。实体中有名为newsId的属性,查询时也应使用相同的属性,如下:WHERE c.newsId(此时查询中有NEWSID)。
        • hm 现在我在路径 [/strts-spring-hbnt] 的上下文中为 servlet [action] 有 classcast 异常 Servlet.service() 抛出异常 [java.lang.ClassCastException: com.epam.testapp .domain.News 无法转换为 com.epam.testapp.domain.News] 根本原因 java.lang.ClassCastException: com.epam.testapp.domain.News 无法转换为 com.epam.testapp.domain.News
        • ok 问题已解决我将 context.xml 中的 ClassLoader 更改为 TomcatInstrumentableClassLoader
        猜你喜欢
        • 2015-04-15
        • 2017-08-25
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        • 2015-04-21
        • 2014-03-07
        • 2023-01-02
        • 2015-09-16
        相关资源
        最近更新 更多