Using resultmap of mybatis to collect objects into a linkedhashmap

1,490 views
Skip to first unread message

Ajay Bodhe

unread,
Sep 9, 2014, 3:15:18 PM9/9/14
to mybati...@googlegroups.com
 

I have two tables: employee_details(department_id, employee_id) & skill_details(employee_id, skill_id). I want to construct LinkedHashMap<Employee_Details> for each department_id, such that

class Employee_Details {
    long employee_id;
    ArrayList<long> skill_ids;
    // other data & code may be
    ----;
    ----; 
}

That means each employee_id for that department_id will result in new Employee_Details object which will be
pushed into LinkedHashMap<Employee_Details>. And all skills for the same employee_id will be stored into
Arraylist<long> skill_ids
field in Employee_Details.

How this can be achieved using resultmaps in mybatis?


Guy Rouillier

unread,
Sep 9, 2014, 5:53:36 PM9/9/14
to mybati...@googlegroups.com
Take a look in the MyBatis User's Guide. You can do everything you are
trying to do, *except* for having MyBatis return a LinkedHashMap. You
can ask it to return a Map, but you can't specify the specific
implementation. If you really insist on LinkedHashMap, you can create
one by wrapping the returned Map in a LinkedHashMap constructor.

On 9/9/2014 3:15 PM, Ajay Bodhe wrote:
>
> <http://stackoverflow.com/questions/25751906/using-resultmap-of-mybatis-to-collect-objects-into-a-linkedhashmap#>
>
>
>
> I have two tables: employee_details(department_id, employee_id) &
> skill_details(employee_id, skill_id). I want to construct
> LinkedHashMap<Employee_Details> for each department_id, such that
>
> |class Employee_Details {
> long employee_id;
> ArrayList<long> skill_ids;
> // other data & code may be
> ----;
> ----;
> }
>
> That means each/employee_id/ for that/department_id/ will result in new/Employee_Details/ object which will be
> pushed into/LinkedHashMap<Employee_Details>/. And all skills for the same/employee_id/ will be stored into/
> Arraylist<long> skill_ids/ field in/Employee_Details/.
>
> |How this can be achieved using resultmaps in mybatis?
>
>
> --
> 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

Ajay Bodhe

unread,
Sep 10, 2014, 5:04:03 AM9/10/14
to mybati...@googlegroups.com
Hi Guy,
I went through http://mybatis.github.io/mybatis-3/sqlmap-xml.html#Result_Maps
this to get an idea of association n collection in resultmap
I am coding first time in java & mybatis, so if possible some help/xml-java-code-snippet would be great.
Thanks,
Ajay

Eduardo Macarron

unread,
Sep 10, 2014, 11:03:25 AM9/10/14
to mybati...@googlegroups.com
I would say that you can specify a LinkedHashMap as a resultType in xml or as a return type for a mapper method. 


To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.

Eduardo Macarron

unread,
Sep 10, 2014, 12:37:40 PM9/10/14
to mybati...@googlegroups.com

Ajay Bodhe

unread,
Sep 11, 2014, 5:09:32 AM9/11/14
to mybati...@googlegroups.com
thnx got it
> 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

Message has been deleted

Ajay Bodhe

unread,
Nov 11, 2014, 1:29:22 PM11/11/14
to mybati...@googlegroups.com
HI Eduardo,

How can you specify the return type as java.util.LinkedHashMap with resultMao?
I need to specify the return type as Employee_Details right?
with that I am getting following error:
                 java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.LinkedHashMap



/*******************************mysql query*****************************/
<select id="getEmployeeIDSkillIDMapping" resultMap="EmployeeIDSkillIDMapper">
             SELECT employeeID, skillID from employee_skillset;
</select>

/*******************************resultMap*****************************/
    <resultMap id="EmployeeIDSkillIDMapper" type="com.Employee_Details">
             <id property="employeeID" column="employeeID"/>
             <collection property="skillIDs" javaType="java.util.ArrayList" column="employeeID" ofType="long">
                 <result column="skillID"/>
             </collection>
</resultMap>

/*******************************interface*****************************/
@MapKey("employeeID")
    public LinkedHashMap<Long, Employee_Details> getEmployeeIDSkillIDMapping();

/*******************************class definition*****************************/
class Employee_Details {
    Long employeeID;
    ArrayList<Long> skillIDs;

    // other data & code may be
    ----;
    ----; 
}
/*******************************pom.xml*****************************/
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-guice</artifactId>
<version>3.3</version>
</dependency>

LET ME KNOW WHAT MISTAKEs I AM DOING.

Thanks

On Wednesday, 10 September 2014 22:07:40 UTC+5:30, Eduardo wrote:
> 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