简体   繁体   English

JPA选择查询与where子句

[英]JPA select query with where clause

I want to write a select statement but can't figure out how to write the where clause... 我想编写一个select语句,但无法弄清楚如何编写where子句...

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. 我想只获取与Im传递给方法的字符串值匹配的行。

In Criteria this is something like: 在Criteria中,这类似于:

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: 在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 见, http://en.wikibooks.org/wiki/Java_Persistence/Querying#Criteria_API_.28JPA_2.0.29

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 其中arg1 - 您的方法String参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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