Java 如何在jpa查询上传递参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18172273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameter on jpa query?
提问by Yubaraj
I have two entities Like SmsOutand SmsIn. The relation between two entities contains OneToManywhere smsIn.idis primary keyand smsOut.sms_in_idis foreign key.
我有两个实体,如SmsOut和SmsIn。两个实体之间的关系包含OneToMany,其中smsIn.id是主键,smsOut.sms_in_id是外键。
Now I want to pass parameter like smsIn.mobileNumber, smsIn.smsTextand so on, on the query
现在我想在查询中传递像smsIn.mobileNumber、smsIn.smsText等参数
SELECT so FROM SmsOut so order by id desc
Following is my database diagram:
以下是我的数据库图:
Edited
已编辑
Following is my code :
以下是我的代码:
String sql = "SELECT so FROM SmsOut so WHERE so.smsInId.mobileNumber =:mobileNumber AND so.smsInId.smsText =:smsText AND so.smsInId.shortCode =:shortCode between so.smsOutDate =:startDate and so.smsOutDate =:endDate order by id desc";
Query query = em.createQuery(sql);
query.setParameter("mobileNumber", mobileNumber);
query.setParameter("smsText", smsText);
query.setParameter("shortCode", shortCode);
query.setParameter("smsOutDate", startDate);
query.setParameter("smsOutDate", endDate);
smsOutList = query.getResultList();
and exception is :
例外是:
SEVERE: line 1:188: expecting "and", found '='
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: expecting "and", found '=' near line 1, column 188 [SELECT so FROM com.f1soft.SMSC.entities.SmsOut so WHERE so.smsInId.mobileNumber =:mobileNumber AND so.smsInId.smsText =:smsText AND so.smsInId.shortCode =:shortCode between so.smsOutDate =:startDate and so.smsOutDate =:endDate order by id desc]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:624)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
Please Help me. Thanks
请帮我。谢谢
采纳答案by chrylis -cautiouslyoptimistic-
You haven't explained the JPA relationship between SmsIn
and SmsOut
, so I'll assume SmsOut
has a getSmsIn()
with a relation on the id
field.
您还没有解释SmsIn
and之间的 JPA 关系SmsOut
,所以我假设在场上SmsOut
有一个getSmsIn()
with 关系id
。
When you have an EntityManager em
, you can call em.createQuery
, which is like SQL prepare
, and then setParameter
:
当您有 时EntityManager em
,您可以调用em.createQuery
,就像 SQL 一样prepare
,然后setParameter
:
TypedQuery<SmsOut> q = em.createQuery("SELECT so FROM SmsOut so WHERE so.smsIn.mobileNumber = :number ORDER BY id DESC");
q.setParameter("number", "12345678");
List<SmsOut> results = q.getResultList();
See the Javadoc for Query
for the different ways you can specify the parameters.
有关指定参数的不同方式,请参阅JavadocQuery
。
回答by Oscar
SELECT so FROM SmsOut so WHERE smsIn.mobileNumber = ? AND smsIn.smsText =? order by id desc
Replace the ? sign with the apropiate values.
更换 ?用适当的值签名。
回答by Rogério Marchiori
between is the problem:
之间是问题:
between so.smsOutDate =:startDate and so.smsOutDate =:endDate
try
尝试
so.smsOutDate between =:startDate and =:endDate