Quantcast

Need your advice for Grouping data

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Need your advice for Grouping data

stylishguy007@gmail.com
Hello,

I have a data like below returning from database. I am using mybatis
in REST web services.

FName  LName Role
Raj Aryan Programmer
Raj Aryan Designer
Raj Aryan Architect

I want to return Object of User that contains first name and last name
and list of Roles.

so that final output will be like

User{
Raj
Aryan
}
Roles{
Programmer
Designer
Architect
}

Please advice.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Need your advice for Grouping data

Eduardo Macarron
Use a nested resultmap.

<select id="myselect" resultMap="user">
select * from users join users_roles join....
</select>

<resultMap id="user" type="my.package.User">
        <result property="fname" column="FName"/>
        <result property="lname" column="LName"/>
        <collection property="roles" ofType="my.package.Rol">
            <result property="name" column="Role"/>
        </collection>
</resultMap>

2012/3/6 [hidden email] <[hidden email]>:

> Hello,
>
> I have a data like below returning from database. I am using mybatis
> in REST web services.
>
> FName  LName     Role
> Raj     Aryan    Programmer
> Raj     Aryan    Designer
> Raj     Aryan    Architect
>
> I want to return Object of User that contains first name and last name
> and list of Roles.
>
> so that final output will be like
>
> User{
> Raj
> Aryan
> }
> Roles{
> Programmer
> Designer
> Architect
> }
>
> Please advice.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Need your advice for Grouping data

stylishguy007@gmail.com
Thanks for the information.
 
I tried same way mentioned by you but I found that results are getting repeated as:
User{
 Raj
 Aryan
 }
 Roles{
 Programmer
  }
 
------
User{
 Raj
 Aryan
 }
 Roles{
 Designer
 }
-----------
User{
 Raj
 Aryan
 }
 Roles{
  Architect
 }
 
 
However, expected result is:
 
User{
 Raj
 Aryan
 }
 Roles{
 Programmer
 Designer
 Architect
 }
I am using mybatis 3.0.4 and mybatis-spring 1.0.1



 
On Mon, Mar 5, 2012 at 11:05 PM, Eduardo Macarron <[hidden email]> wrote:
Use a nested resultmap.

<select id="myselect" resultMap="user">
select * from users join users_roles join....
</select>

<resultMap id="user" type="my.package.User">
       <result property="fname" column="FName"/>
       <result property="lname" column="LName"/>
       <collection property="roles" ofType="my.package.Rol">
           <result property="name" column="Role"/>
       </collection>
</resultMap>

2012/3/6 [hidden email] <[hidden email]>:
> Hello,
>
> I have a data like below returning from database. I am using mybatis
> in REST web services.
>
> FName  LName     Role
> Raj     Aryan    Programmer
> Raj     Aryan    Designer
> Raj     Aryan    Architect
>
> I want to return Object of User that contains first name and last name
> and list of Roles.
>
> so that final output will be like
>

> User{
> Raj
> Aryan
> }
> Roles{
> Programmer
> Designer
> Architect
> }
>
> Please advice.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Need your advice for Grouping data

Eduardo Macarron
I am afraid you are doing something wrong.

Try this:

create table users (
  USER_ID int,
  USER_NAME varchar(20),
  ROLE_NAME varchar(20)
);

insert into users (USER_ID, USER_NAME, ROLE_NAME) values(1, 'User1', 'Role1');
insert into users (USER_ID, USER_NAME, ROLE_NAME) values(1, 'User1', 'Role2');
insert into users (USER_ID, USER_NAME, ROLE_NAME) values(1, 'User1', 'Role3');

        <select id="getUser" resultMap="user">
                select * from users
        </select>

        <resultMap id="user" type="org.apache.ibatis.submitted.basetest.User">
       <result property="id" column="USER_ID"/>
       <result property="name" column="USER_NAME"/>
       <collection property="roles"
ofType="org.apache.ibatis.submitted.basetest.Rol">
           <result property="name" column="ROLE_NAME"/>
       </collection>
        </resultMap>
Loading...