我试图使用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);
}
}
Stacktrace:
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
坦率地说,我对为什么它不能实例化这个类毫无头绪。
答案 0 :(得分:15)
您在构造函数中得到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);
}
}
答案 1 :(得分:9)
另一种可能性是你在构造函数中有主要类型而不是预期的Java对象。
构造函数参数的例子:int
,long
,boolean
,char
..
您应该替换为Integer
,Long
,Boolean
,Char
......