This will work if you upgrade to iBATIS 2.2.0. Older version don't allow this kind of reuse (the property would have to be a collection for older versions). I think that's the issue.
Jeff Butler
On 9/22/06, Jeynes, Paul [CIB-IT] <[EMAIL PROTECTED]> wrote:
Hi,
I'm just getting to grips with iBatis and am trying to retrieve and populate an object which itself contains another object.
The main object is 'Trade', the secondary object is 'TradeStatus'. I refer to the secondary object in the main resultmap by its result map name.
The resultMap is configured as:
<!-- main object -->
<resultMap id="result" class="bdr.test.domain.Trade">
<result property="tradeId" column="trade_id" columnIndex="1"/>
<result property="tradeVersionId" column="trade_version_id" columnIndex="2"/>
<result property="fii" column="fii" columnIndex="3"/>
<result property="traderId" column="trader_id" columnIndex="4"/>
<result property="tradeDate" column="trade_date" columnIndex="5"/>
<result property="tradeInputDate" column="trade_input_date" columnIndex="6"/>
<result property="buySell" column="buy_sell" columnIndex="7"/>
<result property="quantity" column="quantity" columnIndex="8"/>
<result property="consideration" column="consideration" columnIndex="9"/>
<result property="dealtCurrency" column="dealt_currency" columnIndex="10"/>
<result property="dealtSecurityExchRate" column="dealt_security_exch_rate" columnIndex="11"/>
<result property="dealtPrice" column="dealt_price" columnIndex="12"/>
<result property="safeKeep" column="safe_keep" columnIndex="13"/>
<result property="executionDate" column="execution_date" columnIndex="14"/>
<result property="doNotBill" column="do_not_bill" columnIndex="15"/>
<result property="processCode" column="process_code" columnIndex="16"/>
<result property="tradeStatus" resultMap="Trade.tradeStatusMap"/>
</resultMap>
<!-- secondary object -->
<resultMap id="tradeStatusMap" class="bdr.test.domain.TradeStatus">
<result property="tradeStatusCode" column="trade_status_code"/>
<result property="tradeShortDesc" column="short_desc"/>
<result property="tradeLongDesc" column="long_desc"/>
</resultMap>
<!-- simple join to get data for both objects -->
<select id="getTradeById" resultMap="result">
SELECT t.trade_id, t.trade_version_id, t.fii, t.trader_id, t.trade_date, t.trade_input_date,
t.buy_sell, t.quantity, t.consideration , t.dealt_currency,
t.dealt_security_exch_rate, t.dealt_price, t.safe_keep, t.execution_date,
t.do_not_bill, t.process_code,
ts.trade_status_code, ts.short_desc, ts.long_desc
FROM trade t, trade_status_v ts
WHERE t.trade_id = #tradeId#
AND t.trade_version_id = #tradeVersionId#
AND ts.trade_status_code = t.trade_status
</select>
The secondary object is in the primary bean as follows:
...
private String doNotBill;
private TradeStatus tradeStatus;
private String processCode;
...
The secondary bean is:
public class TradeStatus implements Serializable {
/* Private Fields */
private String tradeStatusCode;
private String tradeShortDesc;
private String tradeLongDesc;
public String getTradeStatusCode() {
return tradeStatusCode;
}
public void setTradeStatusCode(String tradeStatusCode) {
this.tradeStatusCode = tradeStatusCode;
}
public String getTradeShortDesc() {
return tradeShortDesc;
}
public void setTradeShortDesc(String tradeShortDesc) {
this.tradeShortDesc = tradeShortDesc;
}
public String getTradeLongDesc() {
return tradeLongDesc;
}
public void setTradeLongDesc(String tradeLongDesc) {
this.tradeLongDesc = tradeLongDesc;
}
}
When I attempt to get the data, the following is reported:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in bdr/test/dao/ibatis/maps/Trade.xml.
--- The error occurred while applying a result map.
--- Check the Trade.result.
--- The error happened while setting a property on the result object.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error instantiating collection property for mapping 'tradeStatus'. Cause: java.lang.ClassCastException: bdr.test.domain.TradeStatus
Caused by: java.lang.ClassCastException : bdr.test.domain.TradeStatus; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in bdr/test/dao/ibatis/maps/Trade.xml.
--- The error occurred while applying a result map.
--- Check the Trade.result.
--- The error happened while setting a property on the result object.
--- Cause: com.ibatis.sqlmap.client.SqlMapException: Error instantiating collection property for mapping 'tradeStatus'. Cause: java.lang.ClassCastException: bdr.test.domain.TradeStatus
Caused by: java.lang.ClassCastException: bdr.test.domain.TradeStatus
Caused by: com.ibatis.sqlmap.client.SqlMapException: Error instantiating collection property for mapping 'tradeStatus'. Cause: java.lang.ClassCastException: bdr.test.domain.TradeStatus
Caused by: java.lang.ClassCastException: bdr.test.domain.TradeStatus
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java :406)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java :802)
Apologies if this has been answered many times before - I've looked for a similar example in the iBatis doc and also looked at the N+1 examples. Am I getting about this the wrong way?
Thanks,
Paul.
========================
Paul Jeynes
CIB Tech - Equities Middle Office
Citigroup Global Markets Limited
* +44 (0) 2890 409213 Citigroup
* +44 (0) 2890 409136 Legacy Bldg
: [EMAIL PROTECTED] N. Ireland Science Park
* Belfast BT3 9DT
========================
Paul Jeynes
CIB Tech - Equities Middle Office
Citigroup Global Markets Limited
* +44 (0) 2890 409213 Citigroup
* +44 (0) 2890 409136 Legacy Bldg
: [EMAIL PROTECTED] N. Ireland Science Park
* Belfast BT3 9DT
