SqlOutParameter out = new SqlOutParameter("out_refcursor",OracleTypes.CURSOR
,new StudentRowMapper());
// some code..
MapSqlParameterSource parameters = createMapSqlParameterSource();
parameters.addValue("in_studentid","101");
Map<String, Object> result = simpleJdbcCall.execute(parameters);
List<Student> students = (List<Student>) result.get("out_refcursor"); // here I get a warning
Definition of execute()
method:
Map<String, Object> org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SqlParameterSource parameterSource)
"Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations.."
The warning on the above line : List<Student> students = (List<Student>) result.get("out_refcursor");
is:
Type safety: Unchecked cast from Object to List
I understand it is only a compile time warning, and Off course I can do a @SuppressWarnings("unchecked")
to suppress it.
Question: But how do I properly cast it?
One way I tried out is
List<Student> students = castObject( result.get("out_refcursor")); @SuppressWarnings("unchecked") private static <T extends List<?>> T castObject(Object obj){ return (T)obj; }
Still I had to put @SuppressWarnings("unchecked")
in the castObject()
method. And I don't know if this is correct way of doing.
Second way I tried is,
List<?> students = castObject( result.get("out_refcursor")); Student student = (Student)students.get(0); private static <T extends List<?>> List<?> castObject(Object obj){ if(obj instanceof List<?>) { return (List<Student>)obj; } return null; }
Any suggestions/advice are welcome.
List<Student>
, you're fine. – Sotirios DelimanolisList<Student>
, So it is clean coding to put @SupressWarnings("unchecked") in that case? The third approach will avoid the warning too, but I felt too much code – spiderman