我正在使用H2数据库来存储和检索自定义对象以及其他一些带有String数据的列。
我能够成功存储它们,但是当我尝试取回对象并将其投射到我的自定义对象中时,会出现错误消息:
java.lang.ClassCastException:package.CustomObject无法转换为package.CustomObject
我正在使用JDBC模板从H2数据库检索数据。下面是我的配置:
@Configuration
public class H2Config {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.username}")
private String jdbcUserName;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean
public DataSource dataSource() {
DataSource dataSource = DataSourceBuilder.create().driverClassName(jdbcDriver).url(jdbcUrl)
.username(jdbcUserName).password(jdbcPassword).build();
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
下面是我从数据库中检索对象的方法:
@Autowired
JdbcTemplate jdbcTemplate;
///////////////////// Way 1 //////////////////////////
public List<HashMap<String,Object>> getAllRetryObjOT(int startSeq, int endSeq) {
String getAllObjs = "select type,obj from Retry where seq between " + startSeq + " and " + endSeq;
List<Map<String, Object>> retryListMap = null;
List<HashMap<String,Object>> retryList = new ArrayList<HashMap<String,Object>>();
try {
retryListMap = jdbcTemplate.queryForList(getAllObjs);
} catch (Exception e) {
logger.error(e);
}
for (Map<String, Object> retryMap : retryListMap) {
retryList.add(new HashMap<String,Object>(){{
put("type",(String) retryMap.get("type"));
put("obj",retryMap.get("obj"));
}});
}
return retryList;
}
///////////////////// Way 2 //////////////////////////
public List<HashMap<String,CustomObject>> getAllRetry(int startSeq, int endSeq){
String getAllObjs = "select * from Retry where seq between ? and ?";
List<HashMap<String,CustomObject>> retryList = new ArrayList<HashMap<String,CustomObject>>();
retryList=jdbcTemplate.execute(getAllObjs,new PreparedStatementCallback<List<HashMap<String, CustomObject>>>() {
@Override
public List<HashMap<String, CustomObject>> doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
List<HashMap<String, CustomObject>> retryListMap = new ArrayList<HashMap<String,CustomObject>>();
ps.setInt(1, startSeq);
ps.setInt(2, endSeq);
ResultSet rs=ps.executeQuery();
while(rs.next()){
HashMap<String,CustomObject> retryObj=new HashMap<String,CustomObject>();
CustomObject custObj=new CustomObject();
custObj=(CustomObject)rs.getObject("obj");
retryObj.put(rs.getString("type"), custObj);
retryListMap.add(retryObj);
}
return retryListMap;
}
});
return retryList;
}
///////////////////// Way 3 //////////////////////////
public List<HashMap<String,CustomObject>> getAllRetryObj(int startSeq, int endSeq) {
String getAllObjs = "select * from Retry where seq between ? and ?";
List<HashMap<String, CustomObject>> retryListMap = null;
List<HashMap<String,Object>> retryList = new ArrayList<HashMap<String,Object>>();
try {
retryListMap = jdbcTemplate.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(getAllObjs);
ps.setInt(1, startSeq);
ps.setInt(2, endSeq);
return ps;
}
}, new ResultSetExtractor<List<HashMap<String, CustomObject>>>() {
@Override
public List<HashMap<String, CustomObject>> extractData(ResultSet rs) throws SQLException, DataAccessException {
List<HashMap<String, CustomObject>> retryListMap = new ArrayList<HashMap<String,CustomObject>>();
while(rs.next()){
HashMap<String,CustomObject> retryObj=new HashMap<String,CustomObject>();
Object custObj=rs.getObject("obj");
retryObj.put(rs.getString("type"),(CustomObject) custObj);
retryListMap.add(retryObj);
}
return retryListMap;
}
});
} catch (Exception e) {
logger.error(e);
}
return retryListMap;
}
在执行查询后的任何给定时间点,当我尝试将从列"obj"
中检索到的obj强制转换为我的CustomObject
时,它会如上所述抛出ClassCastException
。我的类CustomObject是Serializable
(这是事实,因为我能够存储它)。
在调试模式下查看时,我发现检索到的对象已经是CustomObject类型,如Eclipse的“变量”部分所示。我不知道如何前进,我完全陷入了困境。请帮忙。谢谢。