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 Alexey Kalmykov <ak...@openwaygroup.com> on 2008/02/20 17:01:29 UTC

insert/update objects graph

Hi all,

 

I am trying to evaluate iBATIS as an ORM solution for my project. I
really like the overall idea behind iBATIS and its approach to ORM. I
have a question about insert and update operations. I've been
successfully loading graph of objects using resultMaps. But as far as I
understand, I am unable to insert/update a graph of user-defined objects
*using only* iBATIS mapping. Am I right? If yes, was it an original
intention not to implement such feature or just there are some
difficulties in iBATIS design that prevents from doing it? 

 

Actually, I hope that I am wrong and somehow I can insert/update a graph
of objects only using iBATIS mapping :)

Thank you in advance.

 


RE: insert/update objects graph

Posted by Alexey Kalmykov <ak...@openwaygroup.com>.
May be I just misunderstand something. I was reading "iBATIS in Action"
book and found the following statement there:

 

It is not unusual for an object model to include components that also
contain

child objects. For example, an Order object may contain a list or array
of Order-

Item objects that represent the items that were ordered.

Because the iBATIS framework is primarily a SQL mapping tool, it does
not

manage these sorts of relationships when updating the database. As a
result, this

functionality is something that must be handled in the data layer of
your application

rather than in iBATIS.

 

For example, I have the following class representing nodes of the tree:

 

public class TreeNode {

  private long id;

  private List<TreeNode> children = new ArrayList<TreeNode>();

 

  public long getId() {

    return id;

  }

 

  public void setId(long id) {

    this.id = id;

  }

 

  public List<TreeNode> getChildren() {

    return children;

  }

 

  public void setChildren(List<TreeNode> children) {

    this.children = children;

  }

}

 

And I would like to insert it in table TREE_TABLE:

 

create table TREE_TABLE

(

    ID           NUMBER(10)               not null,

    PARENT_ID  NUMBER (10)               null,

    constraint PK_ TREE_TABLE primary key (ID)

)

/

 

alter table TREE_TABLE

    add constraint FK_ TREE_TABLE_REF_3_TAB foreign key  (PARENT_ID)

    references TREE_TABLE (ID)

/

 

The question is how do I configure sql map "treeInsert" in order to make
this code work:

 

 

    TreeNode root = new TreeNode();

    

    TreeNode child1 = new TreeNode();

    TreeNode child2 = new TreeNode();

    TreeNode child3 = new TreeNode();

    

    root.addChild(child1);

    root.addChild(child2);

    

    child2.addChild(child3); 

    sqlMap.insert("treeInsert", treeRoot);

 

 

Best regards, Alexey Kalmykov

OpenWay

________________________________

From: Stephen Boyd [mailto:swboyd@gmail.com] 
Sent: Wednesday, February 20, 2008 7:05 PM
To: user-java@ibatis.apache.org
Subject: Re: insert/update objects graph

 

There shouldn't be any problems with it.  I do it all the time.  Can you
give us an example of your scenario that doesn't seem to work?

On Wed, Feb 20, 2008 at 11:01 AM, Alexey Kalmykov
<ak...@openwaygroup.com> wrote:

Hi all,

 

I am trying to evaluate iBATIS as an ORM solution for my project. I
really like the overall idea behind iBATIS and its approach to ORM. I
have a question about insert and update operations. I've been
successfully loading graph of objects using resultMaps. But as far as I
understand, I am unable to insert/update a graph of user-defined objects
*using only* iBATIS mapping. Am I right? If yes, was it an original
intention not to implement such feature or just there are some
difficulties in iBATIS design that prevents from doing it? 

 

Actually, I hope that I am wrong and somehow I can insert/update a graph
of objects only using iBATIS mapping :)

Thank you in advance.

 

 


Re: insert/update objects graph

Posted by Stephen Boyd <sw...@gmail.com>.
There shouldn't be any problems with it.  I do it all the time.  Can you
give us an example of your scenario that doesn't seem to work?

On Wed, Feb 20, 2008 at 11:01 AM, Alexey Kalmykov <
akalmykov@openwaygroup.com> wrote:

>  Hi all,
>
>
>
> I am trying to evaluate iBATIS as an ORM solution for my project. I really
> like the overall idea behind iBATIS and its approach to ORM. I have a
> question about insert and update operations. I've been successfully loading
> graph of objects using resultMaps. But as far as I understand, I am unable
> to insert/update a graph of user-defined objects **using only** iBATIS
> mapping. Am I right? If yes, was it an original intention not to implement
> such feature or just there are some difficulties in iBATIS design that
> prevents from doing it?
>
>
>
> Actually, I hope that I am wrong and somehow I can insert/update a graph
> of objects only using iBATIS mapping :)
>
> Thank you in advance.
>
>
>

Re: insert/update objects graph

Posted by Larry Meadors <la...@gmail.com>.
Really, we might be able to make it work if we defined the
relationships in the parameter map.

It's just not been done yet.

If you want to take a crack at it, get the code from SVN and do it.

Please note: I am not saying "Do it your damn self!" here, just
encouraging you to try it if you have been thinking on it, and think
you might want to. :-)

We are also kicking around some ideas for iBATIS 3 - if you can find a
generalized solution to this problem, maybe it can be a new feature we
can wrap into it, too.

Larry


On Feb 20, 2008 9:38 AM, Alexey Kalmykov <ak...@openwaygroup.com> wrote:
>
> Thanks, that is what I would like to know. I'm just curious why <select>
> is able to handle object graphs, but insert/update - no?
>
> Best regards, Alexey Kalmykov
> OpenWay
>
>
> -----Original Message-----
> From: Larry Meadors [mailto:larry.meadors@gmail.com]
> Sent: Wednesday, February 20, 2008 7:35 PM
> To: user-java@ibatis.apache.org
> Subject: Re: insert/update objects graph
>
>
> If I understand your question correctly, you want to take (for
> example) an Order object with a list of OrderLine objects and insert
> them by just saying this:
>
>   sqlMap.insert("order.insert", myOrder);
>
> If so, then no, this is not a part of iBATIS.
>
> While that is a common ORM feature, iBATIS is not an ORM, it is a SQL
> mapper - it maps objects to/from SQL, not objects to/from database
> tables.
>
> That said, doing what you are asking is still pretty simple - here's a
> (non-transactional) example:
>
> public insertOrder(Order order){
>   // this assumes that the "order.insert" includes a selectKey element
>   sqlMap.insert("order.insert", order);
>   for(OrderLine ol:order.getOrderLines()){
>     ol.setOrderId(order.getOrderId());
>     sqlMap.insert("orderline.insert", ol);
>   }
> }
>
> If you are using Spring, you add a transaction annotation, and
> ba-da-bing. Done.
>
> For 4 lines of code, you have this instead:
>
>   myOrderDao.insertOrder(myOrder);
>
> Larry
>
>
> On Feb 20, 2008 9:01 AM, Alexey Kalmykov <ak...@openwaygroup.com>
> wrote:
> > I am trying to evaluate iBATIS as an ORM solution for my project. I
> really
> > like the overall idea behind iBATIS and its approach to ORM. I have a
> > question about insert and update operations. I've been successfully
> loading
> > graph of objects using resultMaps. But as far as I understand, I am
> unable
> > to insert/update a graph of user-defined objects *using only* iBATIS
> > mapping. Am I right? If yes, was it an original intention not to
> implement
> > such feature or just there are some difficulties in iBATIS design that
> > prevents from doing it?
> >
> >
> >
> > Actually, I hope that I am wrong and somehow I can insert/update a
> graph of
> > objects only using iBATIS mapping :)
> >
> > Thank you in advance.
> >
> >
>

RE: insert/update objects graph

Posted by Alexey Kalmykov <ak...@openwaygroup.com>.
Thanks, that is what I would like to know. I'm just curious why <select>
is able to handle object graphs, but insert/update - no?

Best regards, Alexey Kalmykov
OpenWay


-----Original Message-----
From: Larry Meadors [mailto:larry.meadors@gmail.com] 
Sent: Wednesday, February 20, 2008 7:35 PM
To: user-java@ibatis.apache.org
Subject: Re: insert/update objects graph

If I understand your question correctly, you want to take (for
example) an Order object with a list of OrderLine objects and insert
them by just saying this:

  sqlMap.insert("order.insert", myOrder);

If so, then no, this is not a part of iBATIS.

While that is a common ORM feature, iBATIS is not an ORM, it is a SQL
mapper - it maps objects to/from SQL, not objects to/from database
tables.

That said, doing what you are asking is still pretty simple - here's a
(non-transactional) example:

public insertOrder(Order order){
  // this assumes that the "order.insert" includes a selectKey element
  sqlMap.insert("order.insert", order);
  for(OrderLine ol:order.getOrderLines()){
    ol.setOrderId(order.getOrderId());
    sqlMap.insert("orderline.insert", ol);
  }
}

If you are using Spring, you add a transaction annotation, and
ba-da-bing. Done.

For 4 lines of code, you have this instead:

  myOrderDao.insertOrder(myOrder);

Larry


On Feb 20, 2008 9:01 AM, Alexey Kalmykov <ak...@openwaygroup.com>
wrote:
> I am trying to evaluate iBATIS as an ORM solution for my project. I
really
> like the overall idea behind iBATIS and its approach to ORM. I have a
> question about insert and update operations. I've been successfully
loading
> graph of objects using resultMaps. But as far as I understand, I am
unable
> to insert/update a graph of user-defined objects *using only* iBATIS
> mapping. Am I right? If yes, was it an original intention not to
implement
> such feature or just there are some difficulties in iBATIS design that
> prevents from doing it?
>
>
>
> Actually, I hope that I am wrong and somehow I can insert/update a
graph of
> objects only using iBATIS mapping :)
>
> Thank you in advance.
>
>

Re: insert/update objects graph

Posted by Larry Meadors <la...@gmail.com>.
If I understand your question correctly, you want to take (for
example) an Order object with a list of OrderLine objects and insert
them by just saying this:

  sqlMap.insert("order.insert", myOrder);

If so, then no, this is not a part of iBATIS.

While that is a common ORM feature, iBATIS is not an ORM, it is a SQL
mapper - it maps objects to/from SQL, not objects to/from database
tables.

That said, doing what you are asking is still pretty simple - here's a
(non-transactional) example:

public insertOrder(Order order){
  // this assumes that the "order.insert" includes a selectKey element
  sqlMap.insert("order.insert", order);
  for(OrderLine ol:order.getOrderLines()){
    ol.setOrderId(order.getOrderId());
    sqlMap.insert("orderline.insert", ol);
  }
}

If you are using Spring, you add a transaction annotation, and ba-da-bing. Done.

For 4 lines of code, you have this instead:

  myOrderDao.insertOrder(myOrder);

Larry


On Feb 20, 2008 9:01 AM, Alexey Kalmykov <ak...@openwaygroup.com> wrote:
> I am trying to evaluate iBATIS as an ORM solution for my project. I really
> like the overall idea behind iBATIS and its approach to ORM. I have a
> question about insert and update operations. I've been successfully loading
> graph of objects using resultMaps. But as far as I understand, I am unable
> to insert/update a graph of user-defined objects *using only* iBATIS
> mapping. Am I right? If yes, was it an original intention not to implement
> such feature or just there are some difficulties in iBATIS design that
> prevents from doing it?
>
>
>
> Actually, I hope that I am wrong and somehow I can insert/update a graph of
> objects only using iBATIS mapping :)
>
> Thank you in advance.
>
>