从map创建querydsl谓词

时间:2015-10-23 14:59:48

标签: spring-data querydsl

我需要帮助创建一个与Spring数据和querydsl一起使用的谓词。我正在将Daos转换为Repositorys。我遇到了一个有动态查询的人。我可以从列表中创建一个谓词,但我迷失在如何从地图创建动态谓词。以下是我要转换的DaoImpl的代码:

 imageCircle.layer.cornerRadius = imageCircle.frame.size.height/2
 imageCircle.layer.borderWidth = 1
 imageCircle.layer.borderColor = UIColor.blueColor().CGColor
 imageCircle.clipsToBounds = true
 self.view.addSubview(imageCircle)

这是相关的生成类:

    public Set<String> getDocumentsByDocumentAssociation(Map.Entry<String,String>[] associations) {
String queryStr = "SELECT da FROM DocumentExternalAssocEntity as da WHERE";
        StringBuilder sb = new StringBuilder(queryStr);

    //loop through inputs.  for first loop, skip appending the OR statements.  Append OR for all others
    for ( int i = 0; i < associations.length; i++ ){
        if ( i > 0 ) {
            sb.append(" OR");
        }
        String whereString = " (da.associationtype = :docAssocType" + i + " AND da.associationvalue = :docAssocValue" + i + ")";
        sb.append(whereString);
    }

    //query when previous loop is done appending
    final Query query = em.createQuery(sb.toString());        
    for( int i = 0; i < associations.length; i++ ) {
        query.setParameter("docAssocType" + i, associations[i].getKey());
        query.setParameter("docAssocValue" + i, associations[i].getValue());
    }

谢谢,如果您需要任何其他信息,请与我们联系

1 个答案:

答案 0 :(得分:0)

这应该这样做:

BooleanExpression expr = null;
for ( int i = 0; i < associations.length; i++ ){
    BooleanExpression innerExpr = 
        documentExternalAssocEntity.associationtype.eq(associations[i].getKey())
            .and(documentExternalAssocEntity.associationvalue.eq(associations[i].getValue()))
    if (expr == null) {
        expr = innerExpr;
    } else {
        expr = expr.or(innerExpr);
    }
}