java - 如何修复 native 查询 : it is showing syntax error near or at 中的错误

标签 java spring spring-boot spring-data-jpa

<分区>

我有一个包含字段 id 、 name 等的表名组,但我只想获取 id 和 name ,所以为此我制作了一个单独的(数据传输对象)。

我写了一个数据库查询来返回数据传输对象,但我不知道为什么它显示错误:语法错误在或接近“。”

@Query(value = "select new 
com.colo.dashboard.api.dto.GroupNameDto(g.id, g.name) from groups 
g", nativeQuery = true)
List<GroupNameDto> getGroupsName();

GroupNameDto:

public class GroupNameDto {

private Long id;
private String name;

public GroupNameDto(Long id, String name) {
    this.id = id;
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}}

实际结果: 附近或处的语法错误。

最佳答案

@Entity
public class GroupName {

private Long id;
private String name;

public GroupName(Long id, String name) {
    this.id = id;
    this.name = name;
}

}

public class GroupNameDto {

private Long id;
private String name;

public GroupNameDto(Long id, String name) {
    this.id = id;
    this.name = name;
}

}

@Query(value="select new com.colo.dashboard.api.dto.GroupNameDto(g.id, g.name) from GroupName g")
List<GroupNameDto> getGroupsName();

关于java - 如何修复 native 查询 : it is showing syntax error near or at 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528640/

相关文章:

java - 双击退出应用程序时出现问题

java - 在 Java 中使用 if/else 处理警报

java - Google App Engine 在没有 maven 的情况下添加模块

java - 如何在 Spring JPA 中使用 SQL View ?

spring - ApplicationEventMulticaster 未使用 Spring HATEOAS 初始化

java - 本地主机/127.0.0.1 :9042] Cannot connect

ios - 在堆栈 View 中动画 subview ?

java - Spring Boot 2.0.0.RELEASE迁移后"Got different size of tuples and aliases"异常

spring-boot - Logback - logback.xml 中的布局和模式

spring-boot - 假装下载文件