繁体   English   中英

无法使用 Hibernate createQuery 实例化类

[英]Can't instantiate class using Hibernate createQuery

我正在尝试使用 hibernate 创建对象,这些对象不是域模型,但我收到一条难以理解的错误消息。

我的Java代码:

public BoxMeasureStat getBoxStat(long box_id, long ) {

    TypedQuery<BoxMeasureStat> data = em.createQuery(
            "SELECT NEW org.massema.util.BoxMeasureStat(MIN(bm.value), AVG(bm.value) , MAX(bm.value), MAX(bm.start_time) ) "
                + "FROM  bm WHERE bm.box_id = :box_id "
                + "AND bm.type_id = :type_id "
                + "order by start_time desc limit 1", BoxMeasureStat.class)
                .setParameter("box_id", box_id)
                .setParameter("type_id", type_id);

    BoxMeasureStat result = data.getSingleResult(); 

    return result;
}

班上:

package org.massema.util;

import java.lang.Float;
import java.util.Date;

import org.joda.time.DateTime;


public class BoxMeasureStat {

    private String type;
    private Float min;
    private Double avg;
    private Float max;
    private String time;
    public BoxMeasureStat( Float min, Double avg, Float max, DateTime time) {
        super();

        this.min = (min);
        this.avg = (avg);
        this.max = (max);
        this.time = time.toString(QueryResultConverter.dateFormatter);
    }


    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Float getMin() {
        return min;
    }
    public void setMin(Float min) {
        this.min = min;
    }
    public Double getAvg() {
        return avg;
    }
    public void setAvg(Double avg) {
        this.avg = avg;
    }
    public Float getMax() {
        return max;
    }
    public void setMax(Float max) {
        this.max = max;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public void setTime(DateTime time) {
        this.time = time.toString(QueryResultConverter.dateFormatter);
    }

}

堆栈跟踪:

IllegalArgumentException occured : org.hibernate.QueryException: could not instantiate class [org.massema.util.BoxMeasureStat] from tuple

play.exceptions.JavaExecutionException: org.hibernate.QueryException: could not instantiate class [org.massema.util.BoxMeasureStat] from tuple
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instantiate class [org.massema.util.BoxMeasureStat] from tuple
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1364)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:312)
    at org.massema.dao.BoxMeasurementsDAO.getBoxStat(BoxMeasurementsDAO.java:509)
    at org.massema.dao.BoxMeasurementsDAO.getBoxStat(BoxMeasurementsDAO.java:492)
    at org.massema.dao.BoxMeasurementsDAO$$FastClassByCGLIB$$f92487e3.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.massema.platform.tx.ISTransactionInterceptor$1.proceedWithInvocation(ISTransactionInterceptor.java:78)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.massema.platform.tx.ISTransactionInterceptor.invoke(ISTransactionInterceptor.java:73)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    at org.massema.dao.BoxMeasurementsDAO$$EnhancerByCGLIB$$5d23bcc4.getBoxStat(<generated>)
    at controllers.API.boxMeasures(API.java:190)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
    ... 1 more
Caused by: org.hibernate.QueryException: could not instantiate class [org.massema.util.BoxMeasureStat] from tuple
    at org.hibernate.transform.AliasToBeanConstructorResultTransformer.transformTuple(AliasToBeanConstructorResultTransformer.java:57)
    at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:95)
    at org.hibernate.loader.hql.QueryLoader.getResultList(QueryLoader.java:458)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
    at org.hibernate.loader.Loader.list(Loader.java:2327)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1247)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:283)
    ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
    at org.hibernate.transform.AliasToBeanConstructorResultTransformer.transformTuple(AliasToBeanConstructorResultTransformer.java:54)
    ... 31 more
Caused by: java.lang.NullPointerException
    at org.massema.util.BoxMeasureStat.<init>(BoxMeasureStat.java:24)
    ... 32 more

表的架构:

CREATE TABLE boxmeasurements
(
  box_id bigint NOT NULL,
  type_id bigint NOT NULL,
  start_time timestamp with time zone NOT NULL,
  value real,
  last_modified timestamp with time zone,
  CONSTRAINT boxmeasurements_pkey PRIMARY KEY (box_id, type_id, start_time),
  CONSTRAINT fkdb5acd82c1990d35 FOREIGN KEY (type_id)
      REFERENCES boxmeasurementtypes (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fkdb5acd82e3f182be FOREIGN KEY (box_id)
      REFERENCES box (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT chk_lastmodified CHECK (date_part('timezone'::text, last_modified) = 0::double precision),
  CONSTRAINT chk_start_time CHECK (date_part('timezone'::text, start_time) = 0::double precision)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE boxmeasurements
  OWNER TO postgres;

版本:

Hibernate: 4.19
Hibernate-jpa-2.0-api
Java: 1.7
PostgreSql: 9.3.5
Ubuntu: 12.04

坦率地说,我不知道为什么它不能实例化这个类。

您在构造函数中得到NullPointerException,因此查询从数据库检索的time参数可能为NULL。 尝试更改构造函数以处理NULL值:

public BoxMeasureStat( Float min, Double avg, Float max, DateTime time) {
    super();

    this.min = (min);
    this.avg = (avg);
    this.max = (max);
    if (time != null) {
        this.time = time.toString(QueryResultConverter.dateFormatter);
    }
}

另一种可能性是你在构造函数中有主要类型而不是预期的Java对象。

构造函数参数的例子: intlongbooleanchar ..你应该用IntegerLongBooleanChar替换......

无法实例化类:这对我来说,我尝试了所有给出的答案,例如将原始值更改为 long 之类的 Long ,也完成了空检查。 但仍然得到同样的错误

public UserExpirationReport(Long partyId, String firstName, String lastName, String companyName, String userName, Date registrationDate, Integer isActive, Date lastLoginDateTime, Date startDate, Date endDate, String ratePlan, String accountType) { super(); if (partyId != null) { this.partyId = partyId; } if (userName != null) { this.userName = userName; } if (firstName != null) { this.firstName = firstName; } if (lastName != null) { this.lastName = lastName; } if (registrationDate != null) { this.registrationDate = registrationDate; } if (isActive != 0) { this.isActive = isActive; } if (companyName != null) { this.companyName = companyName; } if (startDate != null) { this.startDate = startDate; } if (endDate != null) { this.endDate = endDate; } if (accountType != null) { this.accountType = accountType; } if (ratePlan != null) { this.ratePlan = ratePlan; } if (lastLoginDateTime != null) { this.lastLoginDateTime = lastLoginDateTime; } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM