Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to handle inserts/update for collections and associations
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
rogergl  
View profile   Translate to Translated (View Original)
 More options Aug 22 2012, 4:10 pm
From: rogergl <ro...@gilliar.de>
Date: Wed, 22 Aug 2012 13:10:06 -0700 (PDT)
Local: Wed, Aug 22 2012 4:10 pm
Subject: How to handle inserts/update for collections and associations

Hi,

I just started to use myBatis (after using Hibernate now for some years)
and I really like the way to map even complex queries. But now I'm
wondering how I would implement inserts/update of an object containing
collections and
associations.

Example:

<resultMap id="resultMap" type="de.test.Vkprlist">
    <result property="id" column="v_id" />
    <result property="name" column="v_name" />
    <collection property="preise" ofType="de.test.Vkpreise">
        <result property="id" column="p_id" />
        <result property="pzn" column="p_pzn"/>
        <association property="artikel"  resultMap="artikelResult" />
     </collection>
</resultMap>

It seems as If I have to check what objects from the collections must be
inserted or updated (maybe by checking if the id is set ?). This would
require one insert/update statement for the collection, one insert/update
statement for the association and one insert/update for the main object.

Is that true ?

Regards
  Roger


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Butler  
View profile  
 More options Aug 22 2012, 5:41 pm
From: Jeff Butler <jeffgbut...@gmail.com>
Date: Wed, 22 Aug 2012 17:41:48 -0400
Local: Wed, Aug 22 2012 5:41 pm
Subject: Re: How to handle inserts/update for collections and associations

This is true.  MyBatis does not keep track of the state of your objects -
you have to do that yourself.

I usually end up writing a service object that does the work.  The service
object interacts with the mappers for each individual object.  For me,
there's usually a loop that looks like this:

for each child object
   update child object
   if (rows updated == 0)
      insert child object
   end if
end for

If you allow deletes, then the loop might look like this:

delete all children
for each child object
   insert child object
end for

Alternatively you could try to implement some kind of strategy to determine
if the object has changed - maybe set a "dirty flag" with each setter.
 I'll say that every time I've tried to go down that path I've ended up
regretting it.  This is, essentially, what Hibernate does.  They do it in a
systematic way that is less error prone than trying to write it yourself.

Jeff Butler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rogergl  
View profile  
 More options Aug 25 2012, 3:51 pm
From: rogergl <ro...@gilliar.de>
Date: Sat, 25 Aug 2012 12:51:15 -0700 (PDT)
Local: Sat, Aug 25 2012 3:51 pm
Subject: Re: How to handle inserts/update for collections and associations

Hi,

thanks for your answer. I'm glad it is working as I expected it to work ;-)

Regards
  Roger


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »