最近在使用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 -1 ) * limit;
query.setFirstResult(switchPage);
query.setMaxResults(10);
PageModel page = new PageModel();
page.setPageNumber(start);
page.setObjectsPerPage(limit);
page.setFullListSize(Integer.valueOf(query_count.list().get(0).toString()));
page.setData(query.list()); //query.list()作用是执行查询,出现异常
return page;
}
以上代码中的page.setData(query.list());出现异常。
2. 异常信息
1 严重: Servlet.service() for servlet dispatcher threw exception
2 java.lang.IllegalArgumentException
3 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
4 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
5 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
6 at java.lang.reflect.Method.invoke(Method.java:606)
7 at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
8 at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
9 at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
10 at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
11 at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
12 at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
13 at org.hibernate.loader.Loader.doQuery(Loader.java:729)
14 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
15 at org.hibernate.loader.Loader.doList(Loader.java:2220)
16 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
17 at org.hibernate.loader.Loader.list(Loader.java:2099)
18 at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
19 at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
20 at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
21 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
22 at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
23 at com.c503.poss.dao.disaster.DsDisasterDamageDaoImpl.queryByPage(DsDisasterDamageDaoImpl.java:78)
24 at com.c503.poss.service.disaster.DsDisasterDamageServiceImpl.findByCommon(DsDisasterDamageServiceImpl.java:107)
25 at com.c503.poss.service.disaster.DsDisasterDamageServiceImpl.findDisasterDamage(DsDisasterDamageServiceImpl.java:94)
26 at com.c503.poss.ctrl.disaster.DsDisasterDamageController.findDisasterDamage(DsDisasterDamageController.java:218)
27 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
28 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
29 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
30 at java.lang.reflect.Method.invoke(Method.java:606)
31 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
32 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
33 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
34 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
35 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
36 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
37 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
38 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
39 at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
40 at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
42 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
43 at com.c503.poss.ctrl.sm.SecurityFilter.doFilter(SecurityFilter.java:73)
44 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
45 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
46 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
47 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
48 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
49 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
50 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
51 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
52 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
53 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
54 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
55 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
56 at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
57 at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
58 at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
59 at java.lang.Thread.run(Thread.java:745)
3. 数据库相关表字段设计,相关字段为data_size和data_time_length
4. 出问题的实体类为:红色的double字体处出错
1 package com.c503.poss.model.disaster;
2
3 import java.util.Date;
4
5
6 /**
7 * DsImageVideoData entity. @author MyEclipse Persistence Tools
8 */
9
10 public class DsImageVideoData {
11
12
13 // Fields
14
15 private Integer id;
16 private DsTower dsTowerByEndTowerId;
17 private DsLine dsLine;
18 private DsTower dsTowerByStartTowerId;
19 private DsDisaster dsDisaster;
20 private String disasterTypeName;
21 private String dataKind;
22 private String dataName;
23 private String storagePath;
24 private double dataSize;
25 private double dataTimeLength;
26 private String dataFormat;
27 private String dataType;
28 private String phase;
29 private Date time;
30
31
32 // Constructors
33
34 /** default constructor */
35 public DsImageVideoData() {
36 }
37
38 /** minimal constructor */
39 public DsImageVideoData(DsLine dsLine, DsDisaster dsDisaster) {
40 this.dsLine = dsLine;
41 this.dsDisaster = dsDisaster;
42 }
43
44
45 // Property accessors
46
47 public Integer getId() {
48 return this.id;
49 }
50
51 public void setId(Integer id) {
52 this.id = id;
53 }
54
55 public DsTower getDsTowerByEndTowerId() {
56 return this.dsTowerByEndTowerId;
57 }
58
59 public void setDsTowerByEndTowerId(DsTower dsTowerByEndTowerId) {
60 this.dsTowerByEndTowerId = dsTowerByEndTowerId;
61 }
62
63 public DsLine getDsLine() {
64 return this.dsLine;
65 }
66
67 public void setDsLine(DsLine dsLine) {
68 this.dsLine = dsLine;
69 }
70
71 public DsTower getDsTowerByStartTowerId() {
72 return this.dsTowerByStartTowerId;
73 }
74
75 public void setDsTowerByStartTowerId(DsTower dsTowerByStartTowerId) {
76 this.dsTowerByStartTowerId = dsTowerByStartTowerId;
77 }
78
79 public DsDisaster getDsDisaster() {
80 return this.dsDisaster;
81 }
82
83 public void setDsDisaster(DsDisaster dsDisaster) {
84 this.dsDisaster = dsDisaster;
85 }
86
87 public String getDisasterTypeName() {
88 return this.disasterTypeName;
89 }
90
91 public void setDisasterTypeName(String disasterTypeName) {
92 this.disasterTypeName = disasterTypeName;
93 }
94
95 public String getDataKind() {
96 return this.dataKind;
97 }
98
99 public void setDataKind(String dataKind) {
100 this.dataKind = dataKind;
101 }
102
103 public String getDataName() {
104 return this.dataName;
105 }
106
107 public void setDataName(String dataName) {
108 this.dataName = dataName;
109 }
110
111 public String getStoragePath() {
112 return this.storagePath;
113 }
114
115 public void setStoragePath(String storagePath) {
116 this.storagePath = storagePath;
117 }
118
119 public double getDataSize() {
120 return this.dataSize;
121 }
122
123 public void setDataSize(double dataSize) {
124 this.dataSize = dataSize;
125 }
126
127 public double getDataTimeLength() {
128 return this.dataTimeLength;
129 }
130
131 public void setDataTimeLength(double dataTimeLength) {
132 this.dataTimeLength = dataTimeLength;
133 }
134
135 public String getDataFormat() {
136 return this.dataFormat;
137 }
138
139 public void setDataFormat(String dataFormat) {
140 this.dataFormat = dataFormat;
141 }
142
143 public String getDataType() {
144 return this.dataType;
145 }
146
147 public void setDataType(String dataType) {
148 this.dataType = dataType;
149 }
150
151 public String getPhase() {
152 return this.phase;
153 }
154
155 public void setPhase(String phase) {
156 this.phase = phase;
157 }
158
159 public Date getTime() {
160 return this.time;
161 }
162
163 public void setTime(Date time) {
164 this.time = time;
165 }
166 }
5. 改进
将步骤3中实体类中的属性dataSize和dataTimeLength的数据类型给回Double(包括setter和getter中的方法)
【重要参考】:http://www.cnblogs.com/lucky2u/p/3498046.html |