2012-11-21 50 views
0

我已經移植了一些EntityJPA來記錄和現在移植我的一些查詢。 這裏是JPA查詢:下面使用彈簧數據將JPQL移植到mongo的查詢mongo標準

em.createQuery("select distinct c from CustomerImpl c left join fetch c.addresses ca where (:name is null or c.firstName LIKE :name or c.lastName LIKE :name) and (:ref is null or c.externalReference LIKE :ref) and (:city is null or ca.city LIKE :city) order by c.firstName").setParameter("name", name).setParameter("ref", customerRef).setParameter("city", city).getResultList(); 

是我的嘗試:

Criteria orNameCriteria = new Criteria().orOperator(Criteria.where("firstName").is(null), Criteria.where("firstName").is(name), Criteria.where("lastName").is(name)); 
    Criteria orCustomerRefCriteria = new Criteria().orOperator(Criteria.where("externalReference").is(null), Criteria.where("externalReference").regex(customerRef,"i")); 
    Criteria orAddress = new Criteria().orOperator(Criteria.where("addresses.city").is(null), Criteria.where("addresses.city").regex(city, "i")); 
    Query nameq = new Query(new Criteria().andOperator(orNameCriteria,orCustomerRefCriteria,orAddress)); 

該查詢返回零大小數組列表。然後我將orNameCriteria更改爲使用is子句,並確保name變量中包含的數據具有/作爲後綴和前綴。這並不奏效。

但查詢從mongoVueRockMongo客戶:

{ firstName: /SAM/} 

返回數據。

問題1:你怎麼寫like子句與彈簧數據蒙戈Criteria
問題2:是正確的方式來使用或並和條款與criteria

感謝您閱讀

回答

0

Criteria.where( 「田」)正則表達式(模式)應該工作

0

。因爲我沒有能力添加評論...

如果您在Criteria上執行靜態導入,它將使您的where子句看起來好多了。

Criteria orAddress = new Criteria()。orOperator(where(「addresses.city」)。(null),where(「addresses.city」).regex(city,「i」));