我的代码有问题。
我粘贴在我的DAO界面下面:
public interface EventDao extends CrudRepository<Event, Integer>{
...
@Query("SELECT new com.patryk.entity.ActionStatistics(e.action.actionname,sum(e.lengthinmilis)) FROM Event e where e.user = ?1 and e.removed = 0 and year(e.createdate) = year(?2) and month(e.createdate)=month(?2) and e.action.isworkingaction = 1")
public List<ActionStatistics> findMonthlyUserStatisticsByUserAndCreateDay(User user, Date createday);
...
}
ActionStatistics类:
package com.patryk.entity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ActionStatistics {
private String actionname;
private long time;
private String timeString;
public ActionStatistics() {
this.actionname = "actionname";
this.time = 0;
}
public ActionStatistics(String actionname, long time) {
this.actionname = actionname;
this.time = time;
}
public String getActionname() {
return actionname;
}
public void setActionname(String actionname) {
this.actionname = actionname;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getTimeString() {
SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ss");
sdt.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdt.format(new Date(getTime()));
}
public void setTimeString(String timeString) {
this.timeString = timeString;
}
@Override
public String toString() {
return "ActionStatistics{" + "actionname=" + actionname + ", time=" + time + ", timeString=" + timeString + '}';
}
}
我得到这样的错误,我的堆栈跟踪:
2016-12-04 15:49:45.218 ERROR 7224 --- [nio-8084-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: could not instantiate class [com.patryk.entity.ActionStatistics] from tuple; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instantiate class [com.patryk.entity.ActionStatistics] from tuple] with root cause
java.lang.IllegalArgumentException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
...
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.4.jar:8.5.4]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
查询抛出异常:
mysql> select action1_.actionName as col_0_0_, sum(event0_.lengthInSec) as col_1_0_ from Events event0_, Actions action1_ where event0_.actionID=action1_.actionID and event0_.userID=2 and event0_.removed=0 and year(event0_.createDate)=year(now()) and month(event0_.createDate)=month(now()) and action1_.isWorkingAction=1;
+----------+----------+
| col_0_0_ | col_1_0_ |
+----------+----------+
| NULL | NULL |
+----------+----------+
1 row in set (0.00 sec)
mysql>
我在哪里做错了什么?我怎么解决呢?
答案 0 :(得分:0)
将private long time;
更改为private Long time;