Hibernate Native Query With Panache Repository In Reactive Quarkus(With Mutiny)for PostgreSQL Timescale DB

doinxwow  于 2023-06-23  发布在  React
关注(0)|答案(1)|浏览(99)

我正在尝试编写本地查询,以便在quarkus应用程序中使用PostgreSQL Timescale DB的功能。此应用程序使用Munity的React功能。
https://docs.timescale.com/api/latest/hyperfunctions/last/
这是DB的表

CREATE TABLE IF NOT EXISTS telemetry_data
(
    telemetry_data_id serial NOT NULL,
    telemetry_attribute_id integer NOT NULL,
    date_time timestamp without time zone NOT NULL,
    data_value float
);

SELECT create_hypertable('telemetry_data', 'date_time');

CREATE UNIQUE INDEX idx_telemetry_attribute_id_date_time
ON telemetry_data(telemetry_attribute_id, date_time);

ALTER TABLE IF EXISTS public.telemetry_data
    ADD FOREIGN KEY (telemetry_attribute_id)
    REFERENCES telemetry_attribute (telemetry_attribute_id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;

我正在尝试使用本机查询这个应用程序来使用这个时间尺度数据库特性。这是我想要运行的查询。

SELECT 
first(telemetry_data_id, date_time) FILTER(WHERE date_time IS NOT NULL) AS beg_temp,
last(telemetry_data_id, date_time) FILTER(WHERE date_time IS NOT NULL) AS end_temp
FROM telemetry_data
WHERE date_time >= '2023-06-21 14:52:28' 
AND telemetry_attribute_id = 44

这是我在quarkus应用程序中使用的Panache Repository类。如何构建这个查询。但问题是我想用这个还一个单位。

package abc...........;

import abc.....TelemetryData;
import abc.....Result;
import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;

@ApplicationScoped
public class TelemetryDataRepository implements PanacheRepositoryBase<TelemetryData, Integer> {

    private final EntityManager entityManager;

    public TelemetryDataRepository(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

   public Uni<Result> getAbc() {

        String nativeQuery = "SELECT \n" +
                "first(telemetry_data_id, date_time) FILTER(WHERE date_time IS NOT NULL) AS beg_temp,\n" +
                "last(telemetry_data_id, date_time) FILTER(WHERE date_time IS NOT NULL) AS end_temp\n" +
                "FROM telemetry_data\n" +
                "WHERE date_time >= '2023-06-21 14:52:28' \n" +
                "AND telemetry_attribute_id = 44 ";

        return entityManager.createNativeQuery(nativeQuery, Result.class).getSingleResult();

    }

}

我想用这个结果创建Uni对象并返回它是不是不好?有人帮忙吗?谢谢...

ulydmbyx

ulydmbyx1#

问题是你正在注入JPA EntityManager(不是React式的),你应该使用Mutiny.SessionFactoryPanache.withSession
关于Panache:

public Uni<Result> getAbc() {
        String nativeQuery =  ...;

        return Panache.withSession( session -> session
            .createNativeQuery( nativeQuery ).getSingleResultOrNull()
        );
   }
}

使用常规Hibernate Reactive:

...
import org.hibernate.reactive.mutiny.Mutiny;
import jakarta.inject.Inject;

   ...

   @Inject
   Mutiny.SessionFactory sf

   public Uni<Result> getAbc() {
        String nativeQuery =  ...;

        return sf.withSession( session -> session
            .createNativeQuery( nativeQuery ).getSingleResultOrNull()
        );
    }

}

相关问题