gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:13:50 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com