net.sf.json & hibernate problem resolve

JsonConfig jsonConfig = new JsonConfig(); 

//returns empty array and null object
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

// resolve  java.lang.IllegalArgumentException java.sql.Date.getHours(Date.java:177)
jsonConfig.registerJsonBeanProcessor(java.sql.Date.class, new DateJsonValueProcessor());

//resolve  java.sql.SQLException: Positioned Update not supported.
jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
//convert
JSONArray.fromObject(list,jsonConfig);

//hibernate java.sql.date bean process
class DateJsonValueProcessor implements JsonBeanProcessor {

public JSONObject processBean(Object bean, JsonConfig arg1) {
JSONObject jsonObject = null;
        if( bean instanceof java.sql.Date ){
             bean = new Date( ((java.sql.Date) bean).getTime() );
        }
        if( bean instanceof java.sql.Timestamp ){
            bean = new Date( ((java.sql.Timestamp) bean).getTime() );
        }
        if( bean instanceof Date ){
             jsonObject = new JSONObject();
             jsonObject.element("time", ( (Date) bean ).getTime());
        }else{
             jsonObject = new JSONObject( true );
        }
          return jsonObject;
       }
}

你可能感兴趣的:(sql,.net,Hibernate,json,bean)