2016-12-04 116 views
0

我的代碼存在問題。Spring Data JPA無法實例化類

我粘貼下面我D​​AO接口:

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

col_1_0_應該設置什麼變量?如果這是'時間',請嘗試將其更改爲長時間 – BrunoDM

回答

0

我認爲你的time屬性應該是Long來支持null。 Rememeber,原始類型犯規支持空

來源:

private long time;

要:

private Long time;

而且改變getter/setter方法

0

變化private long time;private Long time;

相關問題