JPQL: The state field path cannot be resolved to a valid type

前端 未结 3 1265
孤街浪徒
孤街浪徒 2021-02-15 18:49

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


        
3条回答
  •  滥情空心
    2021-02-15 19:27

    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 ;-)

提交回复
热议问题