如何通过 Query dsl NumberExpression 列进行排序

发布于 2025-01-14 18:44:54 字数 1666 浏览 3 评论 0原文

在mariadb中,我创建了计算用户点和地点点之间距离的函数

    /* create function */
    DELIMITER $$

    CREATE
    FUNCTION `u_st_distance_sphere`(`user_point` POINT, `place_point` POINT)
    RETURNS double
    BEGIN
    return (6371*acos(cos(radians(ST_X(user_point)))*cos(radians(ST_X(place_point)))*cos(radians(ST_Y(place_point))
        -radians(ST_Y(user_point)))+sin(radians(ST_X(user_point)))*sin(radians(ST_X(place_point)))));
    END$$
    DELIMITER ;

要使用该函数,请在CustomDialect类中注册函数并在.yml中应用

public class MariadbCustomDialect extends MariaDB103Dialect {
    public MariadbCustomDialect() {
        super();
        this.registerFunction(
                "u_st_distance_sphere",
                new StandardSQLFunction("u_st_distance_sphere", StandardBasicTypes.DOUBLE)
        );
    }
}
spring:
  ...
  jpa:
    ...
    database-platform: ...global.config.MariadbCustomDialect

我想制作用点计算的距离列并按距离升序排序

//in PlaceCustomRepository
//making filtered JPQLQuery

String pointWKT = String.format("POINT(%s %s)", 
                                 placeFilterParam.getLatitude(),
                                 placeFilterParam.getLongitude());

NumberTemplate<Double> expr = Expressions.numberTemplate(
    Double.class, 
    "u_st_distance_sphere( ST_GeomFromText({0}), ST_GeomFromText({1}) )", 
    pointWKT, 
    place.point);

JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
    .select(
            new QPlaceResponseTest(place.id, 
                                   expr.as("distance")))
    .from(place)
    .orderBy();

如何指定距离列??

谢谢

In mariadb, I created the function to calucate distance between user point and place point

    /* create function */
    DELIMITER $

    CREATE
    FUNCTION `u_st_distance_sphere`(`user_point` POINT, `place_point` POINT)
    RETURNS double
    BEGIN
    return (6371*acos(cos(radians(ST_X(user_point)))*cos(radians(ST_X(place_point)))*cos(radians(ST_Y(place_point))
        -radians(ST_Y(user_point)))+sin(radians(ST_X(user_point)))*sin(radians(ST_X(place_point)))));
    END$
    DELIMITER ;

To use the function, register function in CustomDialect class and apply in .yml

public class MariadbCustomDialect extends MariaDB103Dialect {
    public MariadbCustomDialect() {
        super();
        this.registerFunction(
                "u_st_distance_sphere",
                new StandardSQLFunction("u_st_distance_sphere", StandardBasicTypes.DOUBLE)
        );
    }
}
spring:
  ...
  jpa:
    ...
    database-platform: ...global.config.MariadbCustomDialect

And I want to make distance column that calculated with points and sort by distance ascending order

//in PlaceCustomRepository
//making filtered JPQLQuery

String pointWKT = String.format("POINT(%s %s)", 
                                 placeFilterParam.getLatitude(),
                                 placeFilterParam.getLongitude());

NumberTemplate<Double> expr = Expressions.numberTemplate(
    Double.class, 
    "u_st_distance_sphere( ST_GeomFromText({0}), ST_GeomFromText({1}) )", 
    pointWKT, 
    place.point);

JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
    .select(
            new QPlaceResponseTest(place.id, 
                                   expr.as("distance")))
    .from(place)
    .orderBy();

How to specify distance column??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2025-01-21 18:44:54

我发现使用常量表达式可以工作

JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
    .select(
            new QPlaceResponseTest(place.id, 
                                   expr.as("distance")))
    .from(place)
    .orderBy(new OrderSpecifier<>(Order.ASC, Expressions.constant("distance")));

I found that use a constant expression can work

JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
    .select(
            new QPlaceResponseTest(place.id, 
                                   expr.as("distance")))
    .from(place)
    .orderBy(new OrderSpecifier<>(Order.ASC, Expressions.constant("distance")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文