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
On Wed, Aug 22, 2012 at 4:10 PM, rogergl <ro
...@gilliar.de> wrote:
> 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