与Querydsl一起的JPA谓词

时间:2016-04-13 16:20:55

标签: jpa spring-data-jpa querydsl

我在我的应用程序中使用Querydsl来大大改进查询数据库的代码。 但是,我有一个来自外部服务的JPA谓词(javax.persistence.criteria.Predicate)。 我想混合使用Querydsl创建的查询和外部谓词。例如:

// JPA Predicate from external service
Root<User> root = ...;
CriteriaBuilder cb = ...;
javax.persistence.criteria.Predicate externalPredicate = externalService.filterEmail(root, cb, "%@gmail.com");

// Create Querydsl predicate
BooleanExpression querydslExp = QUser.username.eq("foo");

// Mix predicates (this is what I need)
querydslExp.and(externalPredicate);

另一个解决方案是将Querydsl谓词转换为JPA谓词:

cb.and(querydslExp.toJpaPredicate(), externalPredicate);

有可能吗? 如果是,我怎样才能实现这一目标? 如果不是,是否有另一种混合谓词的解决方案(比如将两者转换为SQL字符串并创建新查询)?

由于

1 个答案:

答案 0 :(得分:0)

使用QueryDSL表达式Visitor返回JPA标准Expression并使用CriteriaBuilderCriteriaQuery作为上下文,这是非常有可能的:

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object> query = criteriaBuilder.createQuery();

javax.persistence.criteria.Expression<?> result = expression.accept(new Visitor<javax.persistence.criteria.Expression<?>, CriteriaBuilder>() {
    @Override
    public javax.persistence.criteria.Expression<?> visit(Constant<?> expr, CriteriaBuilder context) {
        return context.literal(expr.getConstant());
    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(FactoryExpression<?> expr, CriteriaBuilder context) {
        throw new UnsupportedOperationException();
    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(Operation<?> expr, CriteriaBuilder context) {
        javax.persistence.criteria.Expression<?>[] arguments = new javax.persistence.criteria.Expression<?>[expr.getArgs().size()];
        for (int i = 0; i < expr.getArgs().size(); i++) {
            // Visit arguments recursively
            arguments[i] = expr.getArg(i).accept(this, context);
        }

        if (expr.getOperator() instanceof Ops) {
            switch ((Ops) expr.getOperator()) {
                // For example, add more...
                case EQ:
                    return context.equal(arguments[0], arguments[1]);
                default:
                    // Assuming expr.getOperator().name() as function here is not always enough,
                    // it would be better to use a switch on various operation types.
                    return context.function(expr.getOperator().name(), expr.getType(), arguments);
            }
        }

    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(ParamExpression<?> expr, CriteriaBuilder context) {
        return context.parameter(expr.getType(), expr.getName());
    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(Path<?> expr, CriteriaBuilder context) {
        if (expr.getMetadata().isRoot()) {
            // Get query root
            return query.getRoots().stream().filter(r -> r.getAlias().equals(expr.getMetadata().getName())).findAny().get();
        }

        // Get parent path
        javax.persistence.criteria.Path<?> parent = (javax.persistence.criteria.Path<?>) expr.getMetadata().getParent().accept(this, context);
        return parent.get(expr.getMetadata().getName());
    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(SubQueryExpression<?> expr, CriteriaBuilder context) {
        // Defenitely possible
        throw new UnsupportedOperationException();
    }

    @Override
    public javax.persistence.criteria.Expression<?> visit(TemplateExpression<?> expr, CriteriaBuilder context) {
        // No real JPA equivalent exist. ORM-specifically there are possibilities.
        throw new UnsupportedOperationException();
    }
}, criteriaBuilder);