简体   繁体   English

如何在querydsl中使用别名?

[英]how to use alias in querydsl?

I am new to querydsl and already in love with it, but I can't get how to use an alias in this query. 我是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);

my first thought was something like this which throws an error with the generated query as: 我的第一个想法是这样的事情,它对生成的查询抛出错误:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: as near line 3, column 31 [select product from mesoft.meshopframework.model.Product product where locate(?1,product.name) as score > ?2 or locate(?1,product.code) as score > ?2 group by product.baseProd order by score asc] java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:与第3行第31列附近[从mesoft.meshopframework.model.Product产品中选择locate(?1,product.name)的产品按得分>?2或locate(?1,product.code)作为得分>?2,按product.baseProd按得分排序[asc]

could someone show me the coorect way to do this?? 有人可以告诉我coorect的方法吗? I hope it's not necessary for DB to calculate product.name.locate(searchTerm) or the other one again for sorting, 我希望数据库没有必要再次计算product.name.locate(searchTerm)或另一个来进行排序,

thanks 谢谢

JPQL has a more restricted syntax compared to SQL, but I don't think your example would work in SQL either. 与SQL相比,JPQL的语法更加严格,但是我也不认为您的示例也可以在SQL中使用。

You can't use aliases in the where part and for order you will need pick one of the locate expressions or order by both of them. 您不能在where部分中使用别名,而要使用顺序,则需要选择一个定位表达式或两个都按顺序排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM