mysql - 如何在spring data jpa中进行POJO投影以进行 native 查询

标签 mysql spring jpa spring-data-jpa

我有以下 MySQL 查询。我尝试过 spring data jpa 中的接口(interface)投影,但投影字段 id 是 UUID。所以它没有在界面投影中映射。所以我想尝试 POJO 投影,但它不起作用。

SELECT 
    f.receiver_user_id, f.modified_at, f.text
FROM
    (SELECT 
        receiver_user_id, MAX(modified_at) AS modified_at
    FROM
        text_log
    GROUP BY receiver_user_id) AS x
        INNER JOIN
    text_log AS f ON f.receiver_user_id = x.receiver_user_id
        AND f.modified_at = x.modified_at
ORDER BY f.modified_at DESC;

此外,如果建议对上述查询使用 JPA 标准构建器实现,也会很有帮助。

我尝试过以下 Spring Data JPA 存储库查询实现

@Query(value = "SELECT " + 
            "f.receiver_user_id as receiverUserId, f.modified_at as modifiedAt, f.text as text" + 
            "FROM " + 
            "    (SELECT  " + 
            "        receiver_user_id, MAX(modified_at) AS modified_at " + 
            "    FROM " + 
            "        text_log " + 
            "    GROUP BY receiver_user_id) AS x " + 
            "        INNER JOIN " + 
            "    text_log AS f ON f.receiver_user_id = x.receiver_user_id " + 
            "        AND f.modified_at = x.modified_at " + 
            "ORDER BY f.modified_at DESC limit ?1  offset ?2 ", 
            nativeQuery = true)
    List<ITextLog> findTextLog(int l , int f);

    public interface ITextLog {

            public UUID getReceiverUserId();

            public Date getModifiedAt();

            public String getText();

    }

最佳答案

基于这个答案:也许你可以使用 this

@org.hibernate.annotations.Type(type="org.hibernate.type.UUIDCharType")
public UUID getReceiverUserId();

Or

@org.hibernate.annotations.Type(type="uuid-char")
public UUID getReceiverUserId();

关于mysql - 如何在spring data jpa中进行POJO投影以进行 native 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59857498/

相关文章:

php - 为 SELECT...BETWEEN 查询创建正确的日期格式

mysql - 如何对两个相关表中的列求和

java - 无法通过限定符注入(inject)

java - SpringBoot,我如何根据每个 Controller 或请求映射设置 MultipartFile 大小限制

java - 具有生成 com.sun.jersey.spi.inject.Errors 的复合(两部分)主键的 Glassfish/PostgreSQL 表

mysql - 将表 2 中的数据插入表 1,新 ID 以表 1 的最大值开头

php - Code Igniter SQL注入(inject)和查询结构(具有相同列名的动态where子句)

javascript - jquery如何处理 map ?

java - JPA中如何连接多个数据库?

jakarta-ee - JPA:分离附加的只读实体列表