Reputation: 3785
I want to write a select statement but can't figure out how to write the where clause...
My code:
CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery();
query.select(query.from(SecureMessage.class)).where();
This is within a method that I am passing a string to. I want to fetch only the rows that match the value of the string that Im passing to the method.
Upvotes: 16
Views: 75327
Reputation: 43
You can use multiple where clauses with entity manager as below. You need to set parameters to each argument.
entityManager.createQuery("select u from customer u " +
"where u.instituteName=:instituteName " +
"and u.applicationName=:applicationName")
.setParameter("instituteName", cusSubReq.getInstituteName())
.setParameter("applicationName", cusSubReq.getApplicationName()).getSingleResult();
Upvotes: 0
Reputation: 18379
In Criteria this is something like:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SecureMessage> query = cb.createQuery(SecureMessage.class);
Root<SecureMessage> sm = query.from(SecureMessage.class);
query.where(cb.equal(sm.get("someField"), "value"));
In JPQL:
Query query = entityManager.createQuery("Select sm from SecureMessage sm where sm.someField=:arg1");
query.setParameter("arg1", arg1);
See, http://en.wikibooks.org/wiki/Java_Persistence/Querying#Criteria_API_.28JPA_2.0.29
Upvotes: 23
Reputation: 13510
As I understand, the method parameter should be the parameter of the query.
So, should looks like:
Query query = entityManager.getCriteriaBuilder().createQuery("from SecureMessage sm where sm.someField=:arg1");
query.setParameter("arg1", arg1);
where arg1
- your method String parameter
Upvotes: 18