Search code examples
javapostgresqlspring-bootnativequery

Spring Boot Repository Native Query for DTO in Postgresql Issue


I have a problem about writing a native query for dto in Spring Boot with the usage of Postgresql.

Here is the dto shown below.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserCommentsResponse {
    private String placeName;
    private String text;
}

Here is the native query shown below.

@Query(value="SELECT new com.demo.project.dao.UserCommentsResponse(placeName, text) FROM comment c inner join place p on p.id = c.place_id where customer_id = :id", nativeQuery=true)
List<UserCommentsResponse> getUsersComments(int id);

Here is the error message shown below.

org.postgresql.util.PSQLException: ERROR: syntax error at or near "."

How can I fix it?


Solution

  • Try this

    @Query(value="SELECT UserCommentsResponse(placeName, text) FROM comment c inner join place p on p.id = c.place_id where customer_id = :id", nativeQuery=true)
    

    instead of your original query