无法将sql(jpa query)返回的列转换为自定义类

时间:2018-06-18 21:47:02

标签: java spring spring-boot jpa repository

这个问题已被提出,但我无法解决。

上一个问题:How to return a custom object from a Spring Data JPA GROUP BY query

在我的项目中,我有一个定义如下的自定义类,

public class VerticalBarChartDomain {

    private String name;
    private Long value;
    // constructors
    public VerticalBarChartDomain() {
        // TODO Auto-generated constructor stub
    }
    public VerticalBarChartDomain(String name, Long value) {
        super();
        this.name = name;
        this.value = value;
    }
    //getters and setters
    public String getName() {
        return name;
    }
    public Long getValue() {
        return value;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setValue(Long value) {
        this.value = value;
    }

}

在JPA Repository中,我有一个如下查询,

@Query(value="select   new in.net.rserver.domain.VerticalBarChartDomain( DATE(r.start_time), count(distinct(r.ride_id)) ) from ride r where  r.start_time >= date_sub(curdate(), interval 6 day) group by DATE(r.start_time)",nativeQuery=true)
    public List<VerticalBarChartDomain> getRidesPerDayForVBChart();

当我执行此操作时,我收到如下错误,

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.net.yaana.server.rserver.domain.VerticalBarChartDomain(DATE(r.start_time), coun' at line 1
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488) ~[na:na]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2494) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966) ~[mysql-connector-java-5.1.43.jar:5.1.43]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]

如果我错了,请纠正我。

0 个答案:

没有答案