【问题标题】:Spring Data JPA could not instantiate classSpring Data JPA 无法实例化类
【发布时间】:2017-04-19 00:14:42
【问题描述】:

我的代码有问题。

我粘贴在我的 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> 

我在哪里做错了什么?我该如何解决?

【问题讨论】:

  • col_1_0_ 应该设置哪个变量?如果是time,请尝试将其更改为 Long

标签: spring spring-boot spring-data spring-data-jpa


【解决方案1】:

我认为您的 time 属性应该是 Long 以支持 null。记住,原始类型不支持 null

发件人:

private long time;

收件人:

private Long time;

同时更改 getter/setter

【讨论】:

    【解决方案2】:

    private long time; 更改为private Long time;

    【讨论】:

      【解决方案3】:

      当您使用的构造函数中的参数之一是原始类型并且数据库在其位置检索空值时,这是一个非常普遍的错误。

      因此请尝试将映射类中的所有变量从原始数据类型更改为其对应的包装类

      例如在上面的问题陈述中: 首先改变时间的数据类型

      来自

          private long time;
      

      收件人:

          private Long time;
      

      然后改变构造函数

      来自

          public ActionStatistics(String actionname, long time) {
              this.actionname = actionname;
              this.time = time;
          }
      

          public ActionStatistics(String actionname, Long time) {
              this.actionname = actionname;
              this.time = time;
          }
      

      如果此解决方案对任何人有帮助,请告诉我。 谢谢, 手动

      【讨论】:

        猜你喜欢
        • 2018-12-19
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多