Issue
I am trying to Fetch Data from Posgresql DB using Projection Interface but Unable to Map UUID to the Projection correctly. I can always cast it as text or byte and then convert back to UUID but that isn't the most efficient approach for the whole project with multiple queries.
UserProjection.class
import java.util.UUID;
public interface UserProjection {
UUID getUserId();
}
UserTestRepo.class
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface UserTestRepo extends JpaRepository<UserTest, UUID> {
@Query(nativeQuery = true, value = "select user_id as userId from user_test")
public List<UserProjection> getProjectionData();
}
UserTest.class
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
@Table(name = "user_test")
@Entity
public class UserTest {
@Id
@Column(name = "user_id")
@Type(type = "uuid-char")
public UUID userId;
}
Execution Statements :
List<UserProjection> list = userRepo.getProjectionData();
System.out.println(list.get(0).getUserId());
PosgresSQL Structures is a simple UUID entry. I am getting No Dialect mapping for JDBC type: 1111 error.
Solution
Your query: @Query(nativeQuery = true, value = "select user_id as userId from user_test")
is returning a list of userId from user_test table, not a list of UserProjection. So, you can either declare a constructor and project your values there or just take return a list of userId in your query method.
Simply try this:
@Query(nativeQuery = true, value = "select user_id as userId from user_test")
public List<UUID> getProjectionData();
Or
@Query(nativeQuery = false, value = "select new full_dto_path.UserProjection(user_id) from UserTest")
public List<UserProjection> getProjectionData();
Before applying second approach, make sure you have defined your constructor for that single field.
Note: full_dto_path means the fully qualified path of your UserProjection dto.
Hope this will work.
Answered By - user404
Answer Checked By - Cary Denson (JavaFixing Admin)