MyBatis 3.2.8 Select Select columns from a row as a List<Object>

2,194 views
Skip to first unread message

sandeep reddy

unread,
Nov 11, 2014, 6:31:36 PM11/11/14
to mybati...@googlegroups.com

Hi All,
I have a scenario, where I SELECT all  cars from CARS_TABLE and build a Car object. The challenge I'm facing is to build a List<Feature> from a single row. I tried several ways to this, but couldn't get what I needed.

Below is the table and corresponding java class and XML Mapping. I appreciate if someone can help me resolve this.

CAR_TABLE
ID
MANUFACTURER
YEAR
MODEL
FEATURE_X
FEATURE_Y


Car.java
private int id;
private String manufacturer;
private String year;
private String model;
private Features features;


Features.java
private List<Feature> feature;


Feature.java
private featureType;



Mapping


<resultMap id="BaseResultMap" type="com.eval.Car" >

    <id column="ID" property="ID" jdbcType="NUMERIC" />
    <result column="MANUFACTURER" property="manufacturer" jdbcType="VARCHAR" />
    <result column="YEAR" property="year" jdbcType="VARCHAR" />
    <result column="MODEL" property="model" jdbcType="VARCHAR" />
  <collection property="features" resultMap="FeaturesMap">
    </collection>
  </resultMap>
  
  
  <resultMap id="FeaturesMap" type="com.eval.Features" >
     <collection property="phoneRecord" ofType="com.eval.Feature">
    <result property="featureType" column="feature1"/>                    
    <result property="featureType" column="feature2"/>
    </collection>
  </resultMap>
  
 // I believe the above mapping is not the right way as both columns map to the same property and hence result in a single object. All I need is a List of features i.e List<Feature> where feature1 will be one Feature and feature2 will be another Feature.


SELECT QUERY
  <select id="selectCars" resultMap="BaseResultMap" >
    select *
    from CARS_TABLE
  </select>

Guy Rouillier

unread,
Nov 11, 2014, 7:22:36 PM11/11/14
to mybati...@googlegroups.com
select ID, MANUFACTURER, YEAR, MODEL, FEATURE_X from CARS_TABLE
union
select ID, MANUFACTURER, YEAR, MODEL, FEATURE_Y from CARS_TABLE

On 11/11/2014 6:31 PM, sandeep reddy wrote:
>
> Hi All,
> I have a scenario, where I SELECT all cars from CARS_TABLE and build a
> Car object. The challenge I'm facing is to build a List<Feature> from a
> single row. I tried several ways to this, but couldn't get what I needed.
>
> Below is the table and corresponding java class and XML Mapping. I
> appreciate if someone can help me resolve this.
>
> *CAR_TABLE*
> ID
> MANUFACTURER
> YEAR
> MODEL
> FEATURE_X
> FEATURE_Y
>
>
> *Car.java*
> private int id;
> private String manufacturer;
> private String year;
> private String model;
> private Features features;
>
>
> *Features.java*
> private List<Feature> feature;
>
>
> *Feature.java*
> private featureType;
>
>
>
> *Mapping*
>
>
> <resultMap id="BaseResultMap" type="com.eval.Car" >
>
> <id column="ID" property="ID" jdbcType="NUMERIC" />
> <result column="MANUFACTURER" property="manufacturer"
> jdbcType="VARCHAR" />
> <result column="YEAR" property="year" jdbcType="VARCHAR" />
> <result column="MODEL" property="model" jdbcType="VARCHAR" />
> <collection property="features" resultMap="FeaturesMap">
> </collection>
> </resultMap>
> <resultMap id="FeaturesMap" type="com.eval.Features" >
> <collection property="phoneRecord" ofType="com.eval.Feature">
> <*result property="featureType" column="feature1"/> *
> * <result property="featureType" column="feature2"/>*
> </collection>
> </resultMap>
> // I believe the above mapping is not the right way as both columns
> map to the same property and hence result in a single object. All I need
> is a List of features i.e List<Feature> where feature1 will be one
> Feature and feature2 will be another Feature.
>
>
> *SELECT QUERY*
> <select id="selectCars" resultMap="BaseResultMap" >
> select *
> from CARS_TABLE
> </select>
>
> --
> 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

sandeep reddy

unread,
Nov 11, 2014, 7:37:55 PM11/11/14
to mybati...@googlegroups.com

Hello,
That's right but the problem is I shouldn't use anything else other than SELECT * from CARS_TABLE. More specifically, there will be a VIEW created over CARS_TABLE that cannot be changed. The VIEW will be something like SELECT ID, MANUFACTURER, YEAR, MODEL, FEATURE_X from CARS_TABLE with some conditions.

Guy Rouillier

unread,
Nov 12, 2014, 2:40:39 AM11/12/14
to mybati...@googlegroups.com
What happens to FEATURE_Y in that view? You can union multiple queries
of views just as you can of base tables.

On 11/11/2014 7:37 PM, sandeep reddy wrote:
>
> Hello,
> That's right but the problem is I shouldn't use anything else other than
> *SELECT * from CARS_TABLE*. More specifically, there will be a VIEW
> > an email to mybatis-user...@googlegroups.com <javascript:>
> > <mailto:mybatis-user...@googlegroups.com <javascript:>>.
> > For more options, visit https://groups.google.com/d/optout
> <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
>
> --
> 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>.

sandeep reddy

unread,
Nov 12, 2014, 4:31:33 PM11/12/14
to mybati...@googlegroups.com

My bad, its select ID, MANUFACTURER, YEAR, MODEL, FEATURE_X, FEATURE_Y from CARS_TABLE. My question is more of building the List<Feature> from a single row/
>      > <mailto:mybatis-user+unsub...@googlegroups.com <javascript:>>.
>      > For more options, visit https://groups.google.com/d/optout
>     <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
>
> --
> 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

Condor

unread,
Dec 22, 2014, 3:01:18 PM12/22/14
to mybati...@googlegroups.com
I would use the feature1 etc. approach.

Your class can still contain a list of features, but you would write an additional setter for every feature separately.
Reply all
Reply to author
Forward
0 new messages