I use Ibatis 2.3 and I want to map my result set to a Java Object in this way:
My problem is that I have to show the result of some store procedures that change frequently the resultset and I don't want to rebuild the Java code every this resultset columns change.
Is there any other soulution using Ibatis 2.3, If I migrate to MyIbatis 3.x I can solve my problem? Is there any other library that can solve my problems easily.
Below some details on the solutions I found:
For example, having a result set like this
| item_id | type_id | item_name | item_qt | item_um | creation_date |I would like to map the result to a Object
class Item {
public Integer item_id;
public Integer type_id;
public String item_name;
/* contains: item_qt | item_um | creation_date */
public Haspmap others;
}
I tried two strategies:
Map the query using objects that extend Hashmap. Then I override the put method and using the column name I search the corresponding field in the object using reflection. If I don't find the corresponding field I put the value in the hashmap. The problem is that I have to implement all the types checks.
public class ItemsVO3 extends HashMap<String, Object> implements java.io.Serializable{
private BigDecimal objtId;
private Integer entyId;
private HashMap<String, Object> map;
public ItemsVO3(){
}
@Override
public Object put(String key, Object value) {
if(key!=null){
Field filed ;
try {
if( (filed = this.getClass().getDeclaredField(Functions.toCamel(key)))!=null){
//I Have to manage the type of the value field
// Otherwise i get exception
filed.set(this,value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return super.put(key, value);
}
}
The second strategy is using two xml result map the first that maps the knowns field to the Java object and the second that maps the others columns:
<resultMap id="uv_itm_items2" class="com.speedpms.dao.values.ItemsVO3">
<result property="objtId" column="OBJT_ID" jdbcType="NUMERIC" javaType="Integer"/>
<result property="entyId" column="ENTY_ID" jdbcType="NUMERIC" javaType="Integer"/>
<result property="map" resultMap="com.speedpms.dao.items.simpleHashMap"/>
</resultMap>
<resultMap id="simpleHashMap" class="java.util.HashMap">
</resultMap>
<resultMap id="uv_itm_itemTypes" class="com.speedpms.dao.values.ItemTypesVO">
<result property="id" column="ID" jdbcType="NUMERIC" javaType="Integer"/>
<result property="name" column="NAME" jdbcType="VARCHAR" javaType="String"/>
<result property="description" column="DESCRIPTION" jdbcType="VARCHAR" javaType="String"/>
<result property="sessionId" column="SESSION_ID" jdbcType="NUMERIC" javaType="Integer"/>
</resultMap>
Thanks