0

I have the following MySQL query. I have tried interface projection in spring data jpa but the projection field id is UUID. So it is not getting mapped in interface projection. So I want to try POJO projection but it is not working.

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;

Also, it would helpfull if it is suggested using JPA criteria builder implementation for the above query.

I have tried following spring data JPA repository query implementation

@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();

    }

Ashish Bakwad
  • 791
  • 4
  • 15

1 Answers1

0

Based on this answers: Maybe you can use this

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

Or

@org.hibernate.annotations.Type(type="uuid-char")
public UUID getReceiverUserId();
lczapski
  • 4,026
  • 3
  • 16
  • 32