NO.79 JSONObject.fromBean java.sql.Date.getHours IllegalArgumentException问题之解决

--最终解决:将VO中所有 @Temporal(TemporalType.DATE)全部替换为 @Temporal(TemporalType.TIMESTAMP)

 

【现象】

近期某功能需要将一整个VO转化成JSON串,报出以下错误

2012-4-12 11:35:58 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException
at java.sql.Date.getHours(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1332)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.json.JSONObject.fromBean(JSONObject.java:199)

【初步研究】

1.此VO中某日期字段实际上是java.util.Date类型的;

2.又看了一下java.sql.Date  extends java.util.Date 中的 getHours方法

   /**
    * This method is deprecated and should not be used because SQL Date 
    * values do not have a time component.
    *
    * @deprecated
    * @exception java.lang.IllegalArgumentException if this method is invoked
    * @see #setHours
    */
    public int getHours() {
	throw new java.lang.IllegalArgumentException();
    }


 

3.上网查,Hibernate jpa注释@Temporal(TemporalType.DATE)会将此列映射为java.sql.Date

/** 
* Type used to indicate a specific mapping of <code>java.util.Date</code> 
* or <code>java.util.Calendar</code>. 
* 
* @since Java Persistence 1.0 
*/ 
public enum TemporalType { 
/** 
* Map as <code>java.sql.Date</code> 
*/ 
DATE, 

/** 
* Map as <code>java.sql.Time</code> 
*/ 
TIME, 

/** 
* Map as <code>java.sql.Timestamp</code> 
*/ 
TIMESTAMP 
} 


 

【中间弯路】

    重写java.sql.Date ,把里面的会throw new java.lang.IllegalArgumentException();的set、get方法全部干掉——以前经常无奈重写底层包中的类,重写JDK里的类还是第一次干,结果也就第一次发现:这次并没像以往一样会优先选用自己重写的类,CLASSPATH=./;%CLASSPATH%也不行;(--求达人解释原因@_@)

 

【最终解决】 

    将@Temporal(TemporalType.DATE)全部替换为@Temporal(TemporalType.TIMESTAMP),这样属性是一个java.sql.TimeStamp型,它可没有重写getHours方法。另外考虑数据库是Informix,本来Date型就没时分秒的(非time),原VO中原始类型定义又是java.util.Date,因此初步认为没啥不良影响。(个人认为,即使是Oracle的Date,应该也是无影响的,如果year-to-day的,不用的它的时分秒就好了)。

    目前实际应用中暂未发现不良反应。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值