Can a query return only part of the results on a resultMap on Ibatis?

2.4k Views Asked by At

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 ?

2

There are 2 best solutions below

0
On

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.

0
On

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.