Java SQLData - Cast to user object with a list/array?
java, jdbc, mapping, oracle, types
Solution
You'll need to add a type mapping for the type `ACTIVITY_T` as well as the one for `ACTIVITIES_T`. It's not clear from your question whether you've already done this.
Let's assume you've done this and created a class called `Activity` which implements `SQLData` as well. Once you've done that, the following should suffice to read the activity list within `Activities`:
public void readSQL(SQLInput stream, String typeName) throws SQLException {
Array array = stream.readArray();
this.list = new ArrayList<Activity>();
for (Object obj : (Object[])array.getArray()) {
list.add((Activity)obj);
}
}
Problem
I'm learning on how to use SQLData and having an issue with casting back to my object. My Oracle Types looks something like this: ``` CREATE OR REPLACE TYPE activities_t AS OBJECT ( list activity_list_t; ); CREATE OR REPLACE TYPE activity_list_t AS TABLE OF activity_t; CREATE OR REPLACE TYPE activity_t AS OBJECT ( startDate DATE; endDate DATE; ); ``` And my Java looks like this: ``` public class Activities implements SQLData { private String sqlType = "ACTIVITIES_T"; List<Activity> list; // must have default ctor! public Activities() { } public String getSQLTypeName() throws SQLException { return sqlType; } public List getList() { return list; } public void setList(List list) { this.list = list; } public void readSQL(SQLInput stream, String typeName) throws SQLException { Array a = stream.readArray(); // :( } public void writeSQL(SQLOutput stream) throws SQLException { // stream.writeArray(this.list); } } ``` I've tried a few things in readSQL but I am not having much success - what am I missing? I am calling a PLSQL stored procedure which has an OUT parameter of "activities_t" using JDBC: ``` Map map = connection.getTypeMap(); map.put("ACTIVITIES_T", Class.forName("Activities")); connection.setTypeMap(map); callableStatement = connection.prepareCall("{call GET_ACTIVITIES(?)}"); callableStatement.execute(); ``` Thanks! Steve (most of the above is from memory as the code is at work...)