2

I have two entities Like SmsOut and SmsIn. The relation between two entities contains OneToMany where smsIn.id is primary key and smsOut.sms_in_id is foreign key.

Now I want to pass parameter like smsIn.mobileNumber, smsIn.smsText and so on, on the query

SELECT so FROM SmsOut so order by id desc

Following is my database diagram:

enter image description here

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

4

3 回答 3

10

SmsIn您还没有解释and之间的 JPA 关系SmsOut,所以我假设在该字段上SmsOut有一个getSmsIn()with 关系。id

当你有一个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();

有关指定参数的不同方式,请参阅Javadoc 。Query

于 2013-08-11T13:27:14.297 回答
1
SELECT so FROM SmsOut so WHERE smsIn.mobileNumber = ? AND smsIn.smsText =? order by id desc

更换 ?用适当的值签名。

于 2013-08-11T13:15:29.650 回答
0

之间是问题:

between so.smsOutDate =:startDate and so.smsOutDate =:endDate 

尝试

so.smsOutDate between  =:startDate and =:endDate 
于 2017-03-14T19:45:58.053 回答