Spring Data JPA:@Query给出错误java.sql.SQLException:Column' id'未找到

时间:2017-08-18 10:22:51

标签: java mysql spring jpa

我正在尝试运行以下查询

@org.springframework.stereotype.Repository
public interface ReportingParamRepository extends Repository<ReportingParamDAO, Integer> {

    @Query(nativeQuery = true, value = "SELECT student, AVG(height) FROM ReportingParams GROUP BY student")
    public List<ReportingParamDAO> getAverageGroupByStudent();
}

这是DAO

@Entity
@Table(name = "ReportingParams")
@NamedQuery(name = "ReportingParams.findAll", query = "SELECT r FROM ReportingParamDAO r")
public class ReportingParamDAO extends BaseDAO<Integer, ReportingParam> implements DAO<Integer> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "student", nullable = true)
    private StudentDAO studentBean;

    @Column
    private Long height;

    public Integer getId() {
        return id;
    }

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

    public StudentDAO getStudentBean() {
        return studentBean;
    }

    public void setStudentBean(StudentDAO studentBean) {
        this.studentBean = studentBean;
    }

    public Long getHeight() {
        return height;
    }

    public void setHeight(Long height) {
        this.height = height;
    }
}

但是在执行存储库方法时,我收到以下错误

java.sql.SQLException: Column 'id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1077) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2567) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultSet.java:283) ~[commons-dbcp2-2.1.1.jar:2.1.1]
    at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultSet.java:283) ~[commons-dbcp2-2.1.1.jar:2.1.1]
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:789) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2617) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2600) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2424) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...

如果我直接从mysql终端运行查询它工作正常,我得到正确的输出。有人可以帮忙吗?谢谢!!

1 个答案:

答案 0 :(得分:3)

这不起作用。您的查询将获取您要尝试映射到ReportingParamDAO列表的学生对象。