最近在使用Hibernate开发项目,在写好hql语句,并初始化Query对象,执行Query.list()方法时,应用报IllegalArgumentException异常。经网上查询,现已经基本决定原因,是由于在利用hibernate逆向工程生成实体类和映射文件时,数据库字段的numeric类型被逆向成了java实体类中的Doubel类型,然后我觉得这里不应该用包装类,手贱就改成了基本数据类型double,这一改出了问题,数据库中字段的数据类型和java实体类中的属性类型对不上了,出现了IllegalArgumentException异常。解决方法当然是将double类型改回Double类型。我想,如果大家也有这个异常,基本可以断定是由于数据库中的字段的数据类型和java实体类中的属性类型不匹配造成的

  现在将我解决问题的过程给大家描述一下:

1:出问题的代码位置:Query.list()方法

public PageModel queryByPage(String where, int start, int limit) {

        StringBuffer hql = new StringBuffer("SELECT Object(o) FROM DsImageVideoData AS o WHERE 1=1");
StringBuffer hql_count = new StringBuffer("SELECT Count(o) FROM DsImageVideoData AS o WHERE 1=1"); Session session = hibernateTemplate.getSessionFactory().openSession(); //获得hibernate session对象
Query query = session.createQuery(hql.toString()); // 获得对应hql语句的查询对象query
Query query_count = session.createQuery(hql_count.toString()); // 获得对应hql_count语句的查询对象query int pageTmp = start;
int switchPage = (pageTmp - ) * limit;
query.setFirstResult(switchPage);
query.setMaxResults();
PageModel page = new PageModel();
page.setPageNumber(start);
page.setObjectsPerPage(limit);
page.setFullListSize(Integer.valueOf(query_count.list().get().toString()));
page.setData(query.list()); //query.list()作用是执行查询,出现异常
return page;
}

  以上代码中的page.setData(query.list());出现异常。

2. 异常信息

 严重: Servlet.service() for servlet dispatcher threw exception
java.lang.IllegalArgumentException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
at java.lang.reflect.Method.invoke(Method.java:)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:)
at org.hibernate.loader.Loader.doQuery(Loader.java:)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:)
at org.hibernate.loader.Loader.doList(Loader.java:)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:)
at org.hibernate.loader.Loader.list(Loader.java:)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:)
at com.c503.poss.dao.disaster.DsDisasterDamageDaoImpl.queryByPage(DsDisasterDamageDaoImpl.java:)
at com.c503.poss.service.disaster.DsDisasterDamageServiceImpl.findByCommon(DsDisasterDamageServiceImpl.java:)
at com.c503.poss.service.disaster.DsDisasterDamageServiceImpl.findDisasterDamage(DsDisasterDamageServiceImpl.java:)
at com.c503.poss.ctrl.disaster.DsDisasterDamageController.findDisasterDamage(DsDisasterDamageController.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:)
at java.lang.reflect.Method.invoke(Method.java:)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at com.c503.poss.ctrl.sm.SecurityFilter.doFilter(SecurityFilter.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:)
at java.lang.Thread.run(Thread.java:)

3. 数据库相关表字段设计,相关字段为data_size和data_time_length

4. 出问题的实体类为:红色的double字体处出错

 package com.c503.poss.model.disaster;

 import java.util.Date;

 /**
* DsImageVideoData entity. @author MyEclipse Persistence Tools
*/ public class DsImageVideoData { // Fields private Integer id;
private DsTower dsTowerByEndTowerId;
private DsLine dsLine;
private DsTower dsTowerByStartTowerId;
private DsDisaster dsDisaster;
private String disasterTypeName;
private String dataKind;
private String dataName;
private String storagePath;
private double dataSize;
private double dataTimeLength;
private String dataFormat;
private String dataType;
private String phase;
private Date time; // Constructors /** default constructor */
public DsImageVideoData() {
} /** minimal constructor */
public DsImageVideoData(DsLine dsLine, DsDisaster dsDisaster) {
this.dsLine = dsLine;
this.dsDisaster = dsDisaster;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public DsTower getDsTowerByEndTowerId() {
return this.dsTowerByEndTowerId;
} public void setDsTowerByEndTowerId(DsTower dsTowerByEndTowerId) {
this.dsTowerByEndTowerId = dsTowerByEndTowerId;
} public DsLine getDsLine() {
return this.dsLine;
} public void setDsLine(DsLine dsLine) {
this.dsLine = dsLine;
} public DsTower getDsTowerByStartTowerId() {
return this.dsTowerByStartTowerId;
} public void setDsTowerByStartTowerId(DsTower dsTowerByStartTowerId) {
this.dsTowerByStartTowerId = dsTowerByStartTowerId;
} public DsDisaster getDsDisaster() {
return this.dsDisaster;
} public void setDsDisaster(DsDisaster dsDisaster) {
this.dsDisaster = dsDisaster;
} public String getDisasterTypeName() {
return this.disasterTypeName;
} public void setDisasterTypeName(String disasterTypeName) {
this.disasterTypeName = disasterTypeName;
} public String getDataKind() {
return this.dataKind;
} public void setDataKind(String dataKind) {
this.dataKind = dataKind;
} public String getDataName() {
return this.dataName;
} public void setDataName(String dataName) {
this.dataName = dataName;
} public String getStoragePath() {
return this.storagePath;
} public void setStoragePath(String storagePath) {
this.storagePath = storagePath;
} public double getDataSize() {
return this.dataSize;
} public void setDataSize(double dataSize) {
this.dataSize = dataSize;
} public double getDataTimeLength() {
return this.dataTimeLength;
} public void setDataTimeLength(double dataTimeLength) {
this.dataTimeLength = dataTimeLength;
} public String getDataFormat() {
return this.dataFormat;
} public void setDataFormat(String dataFormat) {
this.dataFormat = dataFormat;
} public String getDataType() {
return this.dataType;
} public void setDataType(String dataType) {
this.dataType = dataType;
} public String getPhase() {
return this.phase;
} public void setPhase(String phase) {
this.phase = phase;
} public Date getTime() {
return this.time;
} public void setTime(Date time) {
this.time = time;
}
}

5. 改进

  将步骤3中实体类中的属性dataSize和dataTimeLength的数据类型给回Double(包括setter和getter中的方法)

【重要参考】:http://www.cnblogs.com/lucky2u/p/3498046.html

Hibernate中Query.list()方法报IllegalArgumentException异常的更多相关文章

  1. eclipse中运行 main 方法报错,找不到类

    eclipse (maven 项目)中运行 main 方法报错,找不到类 ** 发现:在 eclipse中的 "Marker" 控制面板中 ,发现问题所在 只要删除 maven 仓 ...

  2. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  3. hibernate中Query的list和iterator区别(续)

    打开cache后query的list和iterator方法区别 将query 的cache打开的话,缓存的是query本身,以hql 生成的 sql ,再加上参数,分页等信息做为key值,而不是que ...

  4. Hibernate 中 load() 方法导致的 noSession 异常

    之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...

  5. hibernate中many-to-one关联时出现ObjectNotFoundException异常

    采用多对一关联,如果一的那端删除了,多的这端无法感知,虽然数据库中可以通过外键配置将多的一端置空,可是在hibernate里面我暂时不知道如何处理. 目前采用的方式: 1.首先,数据库中需要配置好外键 ...

  6. hibernate中Query的list和iterator区别

    1.Test_query_list类 public class Test_query_iterator_list { public static void main(String[] args) { ...

  7. 关于Hibernate中Java实体类加载出现序列化异常

    晚上跟着教程敲网上商城项目的时候(ssh框架写的),碰到了一个问题,就是如题所示的序列化异常问题,这个问题纠结了很久,最后发现了一个解决方法,虽然这篇文章可能几乎不会被人访问到,但是还是要写出来! 其 ...

  8. hibernate 中 query.list()的优化

    2018年3月15日  今天做项目遇到一个需求,问题是在调用query.list()的时候,因为数据也多大概700条左右,查询一次需要30s+,这简直是不能忍,于是开始考虑怎么优化. 1.因为是单表查 ...

  9. .NET 程序在 Windows XP 下调用 SHA512CryptoServiceProvider 方法报 PlatformNotSupportedException 异常

    转自:http://stackoverflow.com/questions/1293905/sha256cryptoserviceprovider-and-related-possible-to-us ...

随机推荐

  1. UVa 1153 Keep the Customer Satisfied (贪心+优先队列)

    题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序, ...

  2. HAService 刨坑

    High availability is a characteristic of a system, which describes the duration (length of time) for ...

  3. linux每天一小步---cp命令详解

    1 命令功能      cp命令用于复制文件或者目录,cp是copy的缩写. 2 命令语法 cp  [参数] 源文件或者目录  目的文件或者目录 3 命令参数 -a  等同于-dRp,保存所有 -d ...

  4. 看图说说JVM GC收集算法

  5. 字符串匹配的KMP算法(转载)

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  6. 終于解決调用wordpress 4.3 xmlrpc api 发布包含分类的文章时返回“抱歉,文章类型不支持您的分类法”错误的問題

    這個問題我找了很多資料都沒有明說是如何解決,后來突發奇想得出我的解決方案如下,所以特此記錄一下: object postId = blogService.NewPost(0,"admin&q ...

  7. ASP.NET Core2基于RabbitMQ对Web前端实现推送功能

    在我们很多的Web应用中会遇到需要从后端将指定的数据或消息实时推送到前端,通常的做法是前端写个脚本定时到后端获取,或者借助WebSocket技术实现前后端实时通讯.因定时刷新的方法弊端很多(已不再采用 ...

  8. ASP.NET Core2使用Autofac实现IOC依赖注入竟然能如此的优雅简便

    初识ASP.NET Core的小伙伴一定会发现,其几乎所有的项目依赖都是通过依赖注入方式进行链式串通的.这是因为其使用了依赖注入 (DI) 的软件设计模式,代码的设计是遵循着“高内聚.低耦合”的原则, ...

  9. Firefox mobile (android) and orientationchange

    Firefox for Android does not support the orientationchange event but you can achieve the same result ...

  10. windows phone 8.0 app 移植到windows10 app笔记

    8.0 public class Convisibility : IValueConverter { public object Convert(object value, Type targetTy ...