Ibatis ResultMap Hashmap

1,511 views
Skip to first unread message

Davide Poletti

unread,
Mar 7, 2014, 3:30:11 AM3/7/14
to mybati...@googlegroups.com

 I use Ibatis 2.3 and I want to map my result set to a Java Object in this way:

  • Map some columns with corresponding properties in the Java object
  • Map others columns into an hashmap field of the object, these columns are not fixed and the could change

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

Guy Rouillier

unread,
Mar 7, 2014, 12:54:21 PM3/7/14
to mybati...@googlegroups.com
Just have iBatis put all the results into a hashmap. Then in your Java
code, extract the 3 separate fields.

You should upgrade to MyBatis. The iBatis code is no longer actively
maintained.

On 3/7/2014 3:30 AM, Davide Poletti wrote:
> I use Ibatis 2.3 and I want to map my result set to a Java Object in
> this way:
>
> * Map some columns with corresponding properties in the Java object
> * Map others columns into an hashmap field of the object, these
> 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
>
> |
>
> --
> You received this message because you are subscribed to the Google
> Groups "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mybatis-user...@googlegroups.com
> <mailto:mybatis-user...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


--
Guy Rouillier

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Reply all
Reply to author
Forward
0 new messages