Spring Boot Repository用于Postgresql中的DTO本地查询问题

huangapple go评论55阅读模式
英文:

Spring Boot Repository Native Query for DTO in Postgresql Issue

问题

我在使用Spring Boot和PostgreSQL时编写DTO的本地查询时遇到了问题。

以下是DTO的定义:

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

以下是本地查询的定义:

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

以下是错误消息:

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

我该如何修复这个问题?

英文:

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=&quot;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&quot;, nativeQuery=true)
List&lt;UserCommentsResponse&gt; getUsersComments(int id);

Here is the error message shown below.

org.postgresql.util.PSQLException: ERROR: syntax error at or near &quot;.&quot;

How can I fix it?

答案1

得分: 1

尝试这个

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

而不是您原始的查询。

英文:

Try this

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

instead of your original query

huangapple
  • 本文由 发表于 2023年1月9日 01:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定