【问题标题】:how to use alias in querydsl?如何在querydsl中使用别名?
【发布时间】:2014-11-22 09:35:47
【问题描述】:

我是 querydsl 的新手并且已经爱上了它,但是我不知道如何在这个查询中使用别名。

    QProduct product = QProduct.product;
    JPQLQuery jPQLQuery = 
            from(product)
            .where(product.name.locate(searchTerm).as("score").gt(0).or(product.code.locate(searchTerm).as("score").gt(0))).groupBy(product.baseProd);
    List<Product> matchedNames = jPQLQuery.orderBy(new NumberPath<Integer>(Integer.class, "score").asc()).offset((pageNumber-1)*10).limit(10).list(product);

我的第一个想法是这样的,它会在生成的查询中引发错误:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌: 在第 3 行附近,第 31 列 [选择产品来自 mesoft.meshopframework.model.Product 产品在哪里 locate(?1,product.name) 作为 score > ?2 或 locate(?1,product.code) 作为 score > ?2 group by product.baseProd order by score asc]

有人可以告诉我这样做的正确方法吗?我希望DB不必再计算product.name.locate(searchTerm)或另一个进行排序,

谢谢

【问题讨论】:

    标签: java mysql hibernate querydsl


    【解决方案1】:

    与 SQL 相比,JPQL 的语法更受限制,但我认为您的示例也不适用于 SQL。

    您不能在 where 部分中使用别名,对于 order,您需要选择其中一个定位表达式或按两者排序。

    【讨论】:

    • 是的,我尝试使用定位表达式进行排序。但是由于我完全是查询世界的新手,而我看到你不是,请问“这是否意味着数据库引擎将再次计算定位表达式以进行排序?以及任何避免这种情况的方法??”
    • 重新计算方面取决于您的数据库引擎。你用的是哪一个?
    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2016-11-16
    • 2021-07-31
    相关资源
    最近更新 更多