Consider the following POJOs that will be used for holding the parameters that will be passed to a query.
public class RegionKey {
private BigDecimal rgnId;
private Country country;
//setters and getters.
}
public class Country {
private BigDecimal cntryId;
//setters and getters.
}
public class Region extends RegionKey {
private String rgnNm;
private String desc;
//setters and getters
}
public class Customer {
private BigDecimal custId;
private Region rgn;
}
Consider the CustomerMapper interface for MyBatis
public interface CustomerMapper {
int deleteByPrimaryKey(@Param("custRecord") Customer key);
}
Consider a snippet from the CustomerMapper.xml file (QUery 1)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
and RGN_ID =
cast(#{custRecord.rgn.rgnId,jdbcType=CHAR} as char(10))
</delete>
The above query works perfectly fine. Modifying the above query with the following if-test works fine as well (Query 2)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
<if test="custRecord.rgn.rgnId != null">
and RGN_ID = cast(#{custRecord.rgn.rgnId,jdbcType=CHAR} as
char(10))
</if>
</delete>
Modifying the query as follows causes a runtime exception (Query 3)
<delete id="deleteByPrimaryKey">
delete from CUSTOMER
where CUST_ID = #{custRecord.custId,jdbcType=DECIMAL}
<if test="custRecord.rgn.country.cntryId != null">
and CNTRY_ID =
cast(#{custRecord.rgn.country.cntryId,jdbcType=CHAR} as
char(10))
</if>
</delete>
I get a org.apache.ibatis.ognl.NoSuchPropertyException at runtime for query number 3. I am unable to understand why this happens. If I can access the rgnId field from custRecord.rgn in query 2, I should technically be able to access the cntryId field from custRecord.rgn.country in query number 3.