Firstly, thanks for such a great product. I just started using
iBatis.NET, but already feel in love with its power and simplicity.
That being said, after studying your documentation sections on complex
types, solving the N+1 issue, etc., I am still having troubleb
understanding how to deal with the following issue.
Suppose my program only has two classes, Stock and Position.
class Stock
{
public string ticker;
public string price;
}
class Position
{
public Stock stock;
public double quantity;
}
Also assume there are two database tables which can be used to
populate the above.
CREATE TABLE Stock (ticker varchar, price float)
CREATE TABLE Position (ticker varchar, quantity int)
My approach thus far has been to use the following result maps and statement.
<resultMap id="Stock_Result" class="MyApp.Stock">
<result property="ticker" column="ticker" />
<result property="price" column="price" />
</resultMap>
<resultMap id="Position_Result" class="MyApp.Position">
<result property="stock" resultMapping="Stock_Result" />
<result property="quantity" column="quantity" />
</resultMap>
<statements>
<select id="GetPositions" resultMap="Position_Result">
SELECT p.ticker, p.quantity, s.price
FROM Position p
JOIN Stock s ON p.ticker = s.ticker
</select>
</statements>
If I call method QueryForList with this statement, things seem to work
fine. I get back a list of Position objects, each containing a
fully-formed Stock object.
My problem is that each Stock object is obviously independent. If I
have multiple positions in say, IBM, I will have duplicate Stock
objects. I am looking for a way to create all of the Position objects
(each containing a reference to a Stock object), but only create new
Stock objects when a new ticker is encountered. In other words, if I
have two positions in IBM, I want to have them both point to the same
Stock object.
Is there any way to accomplish this using iBatis.NET? I've looked at
groupby, but can't seem to make it work this way. Any help or advice
is greatly appreciated!
- Clyde