1. Yes, you have to implement TypeHandler<T> interface.
public interface TypeHandler<T> {
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)
throws SQLException;
public T getResult(ResultSet rs, String columnName)
throws SQLException;
public T getResult(CallableStatement cs, int columnIndex)
throws SQLException;
}
If you are not returning anything from your procedure, you can leave two getResult methods empty.
2. No, you have to do something like this:
If these are the POJO properties:
IdReservation
IdBranch
IAgn
NapAgn
...then you have to do something like this:
List<Map<String,Object>> listOfPojoMaps = new ArrayList<Map<String,Object>>();
Map<String,Object> pojoMap = new LinkedHashMap<String,Object>();
// keys correspond to the properties of the I_RESERVATION_TYPE
pojoMap.put("ID_RESERVATION",pojo.getIdReservation);
pojoMap.put("ID_BRANCH",pojo.getIdReservation);
pojoMap.put("ID_AGN",pojo.getIdReservation);
pojoMap.put("NAP_AGN",pojo.getIdReservation)
listOfPojoMaps.add(pojoMap);
The listOfPojoMaps is the property you set as an input parameter in your stored procedure call.
3. Every map from the listOfPojoMaps creates one STRUCT object. Since we have I_REZERVATION_ARRAY as an array of objects in the database,we have to create STRUCT[] array which we pass as a parameter to the prepairedStatement. If we didn't have that, we could send only one parameter/pojo to the database.
Remember, you are not sending pojos to typehandler, just it's values wrapped in a list of maps.
4. In essence, yes, you will have to create typeHandler for your every type in the database, although you can do something like this:
public class StoredProcedurePojo {
private ArrayList<LinkedHashMap<String, Object>> listOfPojoMaps;
private String typeName;
private String arrayName;
//getters / setters
}
You can send this wrapper as an input parameter to your typeHandler but you have to modify the code snippet mentioned in my first post to:
StoredProcedurePojo wrapperPojo = (StoredProcedurePojo) passedValue;
List<LinkedHashMap<String,Object>> inParams = wrapperPojo.getListOfPojoMaps();
STRUCT[] structArray = new STRUCT[inParams.size()];
conn = ps.getConnection();
StructDescriptor structDesc = StructDescriptor.createDescriptor(
wrapperPojo.getTypeName(), conn);
ArrayDescriptor arrayDesc = ArrayDescriptor.createDescriptor(
wrapperPojo.getArrayName(), conn);
P.S.
It is important that you use ojdbc6 oracle driver, ojdbc14 doesn't seem to work...
I' m not sure if this way is the simplest or the best approach, but it works in our environment...