4

There is some code using Ibatis 2.3, and I have a class User and a resultMap as follows:

public class User {
  private Integer id;
  private String name;

  public Integer getId() {
    return this.id;
  }

  public void setId(final Integer id) {
    this.id = id;
  }

  public String getName() {
    return this.name;
  }

  public void setName(final String name) {
    this.name = name;
  }
}

<resultMap id="userResultMap" class="user">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
</resultMap>

Then I have a select query that only returns the id:

<select id="getUserId" resultMap="userResultMap">
    select id from Foo
</select>

Like that, Ibatis wants to fill in all the results on the resultMap and since "name" is not returned by the query it sends and error:

--- The error occurred in ibatis/user.xml.  
--- The error occurred while applying a result map.  
--- Check the user.userResultMap.  
--- Check the result mapping for the 'name' property.  
--- Cause: java.sql.SQLException: Column 'name' not found.

Is it possible somehow to have queries that only return part of the results on a resultMap ?

Francisco
  • 41
  • 2
  • Its not the problem with mybatis side. Check if you have column with name 'name' – Karthik Prasad May 22 '14 at 07:18
  • The query does not return a column 'name', that's exactly the issue. I can't change the query, it's owned by someone else and it is in fact a call to a procedure in Oracle. Foo is not a table. The query look more like: 'select id from app.getid() ...' – Francisco May 24 '14 at 15:24
  • Then you have to remove property from resultmap as mybatis cannot find the table with column 'name' – Karthik Prasad May 26 '14 at 11:50
  • 1
    That's exactly my question, if there is a way to reuse the same ResultMap on different SQL's, but telling Ibatis when a SQL's will not return one of the columns. Something like "setup" that property with a default/null value when a column does not exists on the SQL. – Francisco May 27 '14 at 17:01
  • one way is using inheritance. But I do not see the real advantage of reuse-ability in this case. – Karthik Prasad May 27 '14 at 18:47
  • Either you have to change the query or you have to change the resultMap – Saif Aug 12 '14 at 09:58

2 Answers2

1

Your select query should be

<select id="getUserId" resultMap="userResultMap">
     select id, name from Foo
</select>

You are missing "name" in your query. It's simple. Your result map has two properties but you only select only one property. It should be the same exactly. Hope it work.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
0

In case of blank or null value you can set some default value for that sql column in sql query itself so that it always returns that coulmn with value.