简体   繁体   中英

How to use where clause in jpa

I have a query Select * from uploadedFile where uid="tesetetesdsfjdsfj";

I want to write this query in jpa and i have tried this

    CriteriaQuery<UploadedFile> query = em.getCriteriaBuilder().createQuery(UploadedFile.class);

    Root<UploadedFile> product = query.from(UploadedFile.class);
    return em.createQuery(query)
         .setFirstResult(0) // offset
         .setMaxResults(10) // limit
         .getResultList();

And i dont know how add where condition in this query ( where uid="tesetetesdsfjdsfj"; )

Can be achieved with following snippet:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<UploadedFile> cq = cb.createQuery(UploadedFile.class);
    Root<UploadedFile> product = cq.from(UploadedFile.class);
    cq.select(product);
    cq.where(cb.equal(
         // you can replace product.get(UploadedFile_.uid) with product.get("uid") 
         product.get(UploadedFile_.uid), "tesetetesdsfjdsfj"));
    em.createQuery(cq)
             .setFirstResult(0) 
             .setMaxResults(10) 
             .getResultList();

More info on using the API can be found here

Can be achieved with following snippet:

 Query query = entityManager.getCriteriaBuilder().createQuery("from UploadedFile fm where sm.someField=:arg1");
    query.setParameter("arg1", arg1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM