0

Requirement: I need to fetch partial data from two queries and populate the data into single resultmap.

Problem: When we get data from one query it is able to load the data into resultmap, but when I load the data from second query into the same result map the data is getting refreshed.

I guess it is creating a new map when we use same resultmap.Is there a way to make a resultmap available through out the session in mapper.xml.

Code

    public Details { 
      private Term term;
    }

    public Term {
       private String name;
       private String location;
    }

mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
            <!DOCTYPE mapper
              PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="org.project.example.MyMapper">
              <select id="select_form" parameterType="long" resultMap="select-result">
                    select id,name from details where id= #{id}
              </select>
              <select id="select_form_ext" parameterType="long" resultMap="select-result-ext">
                    select id,location from term where id= #{id}
               </select>
               <resultMap id="select-result" type="DetailsDto">
                        <association property="TermDto" column="ID"  select="select_form_ext"  />
                        <association property="TermDto" column="ID"  resultMap="select-main-result"  />
               </resultMap>                   
               <resultMap id="select-result-ext" type="TermDto">
                      <result property="location" column="LOCATION" />
               </resultMap>                      
               <resultMap id="select-main-result" type="TermDto" >
                       <result property="name" column="NAME" />
               </resultMap>
    </mapper>
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68

1 Answers1

0

Please give a try using the below steps.

Step1#: Remove the below line <association property="TermDto" column="ID" resultMap="select-main-result" /> from <resultMap id="select-result" >...

Step2#: Modify the ResultMapselect-result-ext as below

<resultMap id="select-result-ext" type="TermDto">
        <result property="location" column="LOCATION" />
        <result property="name" column="NAME" />
</resultMap>

Step3#: Execute/Run the application and check the Results.

Eg#:

<resultMap id="blogResult" type="Blog">
  <association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>

<select id="selectBlog" resultMap="blogResult">
  SELECT * FROM BLOG WHERE ID = #{id}
</select>

<select id="selectAuthor" resultType="Author">
  SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68
  • yeah I have tried the option it only renders the value location and the property name is always null @09Q71A0534 – Kishore Kanumalla May 24 '16 at 09:26
  • Can you run your app in debug mode and check the Dynamic SQL Statement which was generated producing results or not – 09Q71AO534 May 24 '16 at 12:57
  • Check whether the column names were matched or not with the SQL Statement which is executed to the `column="NAME"` & `column="LOCATION"` – 09Q71AO534 May 24 '16 at 13:05
  • @KishoreKanumalla Did those suggestions helped you out fixing the problem ? – 09Q71AO534 Jun 16 '16 at 12:56
  • No sir I tried all the options it didn't worked out for me – Kishore Kanumalla Jun 16 '16 at 15:25
  • hmm.. Can you please debug your code and edit the question with the queries which were generated at run-time. It would be helpful to find the root cause of your problem. The above *Eg#* is working perfect in my machine.Possible can you copy paste the DTO objects as well... – 09Q71AO534 Jun 20 '16 at 07:03
  • Hi sir thanks for your help.I did workaround solution so currently I am not looking into this issue anymore – Kishore Kanumalla Jun 22 '16 at 11:25