You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by xiuxiu <xi...@gmail.com> on 2006/10/27 03:15:15 UTC

how to deal with one-to-many relationship?

Hi, All,

    I am a newer to ibatis And met some problems when I deal with one-to-many relationship. 
    There are two classes:
public class Order {
      private int id;
      private double total;
    private Set orderLineItems = new HashSet();
}
public class OrderLineItem
{
    private int id;
    private int lineItemPrice;
    private Order order;
}

One Order instance contains a group of OrderLineItem instances;
I use resultMap to do the select operation such as :

<resultMap id="get-order-result" class="order">
  <result property="id" column="order_id" />
  <result property="total" column="total" />
  <result property="userName" column="username" />
  <result property="orderLineItems" column="order_id"
   select="getItemByOrderId" />
 </resultMap>
<select id="findOrderById" resultMap="get-order-result"
  parameterClass="java.lang.Integer">
  select order_id,
         total,
         username
  from   orders 
  where  order_id = #id#
 </select>
 
 <select id="getItemByOrderId" resultClass="orderLineItem"
  parameterClass="int">
  select orderlineitem_id,       
         lineitemprice,
         description
  from   orderlineitem
  where  order_id = #id#       
 </select>

but I couldn't find the way to set OrderLineItem.order.
Please have a look and help me if possible.

Luo
    




xiuxiu
2006-10-27

Re: how to deal with one-to-many relationship?

Posted by 罗霄 <xi...@gmail.com>.
It is an example for hibernate to handle one-to-many problem.
when I try to use ibatis to reimplement it , I met this kind of problem that
the OrderLineItem can't get the referece of the Order.Actually, I usually do
not need the child list objects to hold reference to the parent
object.either.


Thank you, Nathan Maves. What you mentioned are do help me for my
development.


2006/10/28, Nathan Maves <na...@gmail.com>:
>
> You are correct. You can not achieve this though the xml configuration.
>
> On 10/27/06, Koka Kiknadze <226057@gmail.com > wrote:
> >
> > Nathan,
> >
> > I think the problem here is not the N+1 one. The Order object has a list
> > of OrderLineItem objects. But one may want each instance of OrderLineItem
> > to hold reference to the parent Order object...
> >
> > IMHO there's no way to achieve it using only XML file. I recall I've
> > used workaround like this:
> >
> > immediatly after calling get findOrderById(String oredrID) i
> > programmatically looped through resultOrder.getOrderLineItems() and set
> > order to resultOrder.
> >
> > Although usually I do not need the child list objects to hold reference
> > to the parent object, I believe it will be handy feature to make it possible
> > without such extra loops (or custom rowhandlers...)
> >
> > Koka
> >
>
>

Re: how to deal with one-to-many relationship?

Posted by Nathan Maves <na...@gmail.com>.
You are correct. You can not achieve this though the xml configuration.

On 10/27/06, Koka Kiknadze <22...@gmail.com> wrote:
>
> Nathan,
>
> I think the problem here is not the N+1 one. The Order object has a list
> of OrderLineItem objects. But one may want each instance of OrderLineItem
> to hold reference to the parent Order object...
>
> IMHO there's no way to achieve it using only XML file. I recall I've used
> workaround like this:
>
> immediatly after calling get findOrderById(String oredrID) i
> programmatically looped through resultOrder.getOrderLineItems() and set
> order to resultOrder.
>
> Although usually I do not need the child list objects to hold reference to
> the parent object, I believe it will be handy feature to make it possible
> without such extra loops (or custom rowhandlers...)
>
> Koka
>

Re: how to deal with one-to-many relationship?

Posted by Koka Kiknadze <22...@gmail.com>.
Nathan,

I think the problem here is not the N+1 one. The Order object has a
list of OrderLineItem
objects. But one may want each instance of OrderLineItem to hold reference
to the parent Order object...

IMHO there's no way to achieve it using only XML file. I recall I've used
workaround like this:

immediatly after calling get findOrderById(String oredrID) i
programmatically looped through resultOrder.getOrderLineItems() and set
order to resultOrder.

Although usually I do not need the child list objects to hold reference to
the parent object, I believe it will be handy feature to make it possible
without such extra loops (or custom rowhandlers...)

Koka

Re: how to deal with one-to-many relationship?

Posted by Nathan Maves <na...@gmail.com>.
First off as a new user please take some time to read through the Developers
Guide that can be found on the main page.  Second what you are doing is the
classic problem of 1:M.  Your current code also has the N+1 problem.  You
will query once for you main select and N number of times for each row the
first query returns.  Once again please consult the developers guide.  It
has examples of how to avoid and solve these problems.

Nathan

On 10/26/06, xiuxiu <xi...@gmail.com> wrote:
>
>  Hi, All,
>
>     I am a newer to ibatis And met some problems when I deal with
> one-to-many relationship.
>     There are two classes:
> public class Order {
>       private int id;
>       private double total;
>     private Set orderLineItems = new HashSet();
> }
> public class OrderLineItem
> {
>     private int id;
>     private int lineItemPrice;
>     private Order order;
> }
>
> One Order instance contains a group of OrderLineItem instances;
> I use resultMap to do the select operation such as :
>
> <resultMap id="get-order-result" class="order">
>   <result property="id" column="order_id" />
>   <result property="total" column="total" />
>   <result property="userName" column="username" />
>   <result property="orderLineItems" column="order_id"
>    select="getItemByOrderId" />
>  </resultMap>
> <select id="findOrderById" resultMap="get-order-result"
>   parameterClass="java.lang.Integer">
>   select order_id,
>          total,
>          username
>   from   orders
>   where  order_id = #id#
>  </select>
>
>  <select id="getItemByOrderId" resultClass="orderLineItem"
>   parameterClass="int">
>   select orderlineitem_id,
>          lineitemprice,
>          description
>   from   orderlineitem
>   where  order_id = #id#
>  </select>
>
> but I couldn't find the way to set OrderLineItem.order.
> Please have a look and help me if possible.
>
> Luo
>
>
>  ------------------------------
>  xiuxiu
> 2006-10-27
>