How to set parameters for a Named Query

前端 未结 1 1444
生来不讨喜
生来不讨喜 2021-02-11 07:01

Hi I have a Named Query

@NamedQuery(name = \"StudyplanCategory.findByStatusAndLimit\",
            query = \"SELECT s FROM StudyplanCategory s WHERE 
           


        
1条回答
  •  温柔的废话
    2021-02-11 07:36

    As @DataNucleus said, LIMIT is not a valid keyword in JPQL. That's not the way to specify how many rows to return. This is how you do it:

    @NamedQuery(name = "StudyplanCategory.findByStatusAndLimit", 
            query = "SELECT s FROM StudyplanCategory s WHERE 
                        s.status =:status")
    

    And this would be the code to call the named query:

    Query query = entityManager.createNamedQuery("StudyplanCategory.findByStatusAndLimit");
    int end=(start*pageNumber);
    query.setParameter("status", status);
    query.setFirstResult(start);
    query.setMaxResults(end - start);
    return (List) query.getResultList();
    

    0 讨论(0)
提交回复
热议问题