emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

add resultMap attribute to constructor/arg element #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Reporter:    Ostap Bender

Hi All,

I guess this is a corner case but it is pretty important one for projects
with legacy data models.
Let say there is a table in DB that contains:

Column Type
id int
s1 string
s2 string
s3 string

Also let say it is modeled (historically) by two immutable objects:

public class ObjX
{
private final int id;
private final String s1;
private final ObjY objY;

public ObjX( int id, String s1, ObjY objY )
{
...
}
}

public class ObjY
{
private final String s2;
private final String s3;

public ObjY( String s2, String s3 )
{
...
}
}

In this case there seems to be no way to build a result map for ObjX in
iBatis without changing ObjX code but what if we cannot change it? And the
requirement is to create objects via constructors only.

It would be nice if we could use resultMap in <constructor>/<arg> element.
This way one could write something like:

<resultMap type="ObjX" id="ObjXResult">
        <constructor>
            <idArg column="id" javaType="_int" />
            <arg column="s1" javaType="String" />
            <arg javaType="ObjY" resultMap="ObjYResult"/>
        </constructor>
</resultMap>

<resultMap type="ObjY" id="ObjYResult">
        <constructor>
            <arg column="s2" javaType="String" />
            <arg column="s3" javaType="String" />
        </constructor>
</resultMap>

Comparable result could be achieved with TypeHandler but it will require
column names to be hard-coded in the TypeHandler implementation also will
require adding one TypeHandler per each such a case.

Original issue reported on code.google.com by clinton....@gmail.com on 17 May 2010 at 1:38

GoogleCodeExporter commented 9 years ago

Original comment by clinton....@gmail.com on 17 May 2010 at 1:38

GoogleCodeExporter commented 9 years ago

Original comment by clinton....@gmail.com on 17 May 2010 at 3:21

GoogleCodeExporter commented 9 years ago

Original comment by clinton....@gmail.com on 2 Jun 2010 at 4:45

GoogleCodeExporter commented 9 years ago
can we bump this feature please. it's extremely helpful for people who have 
immutable classes which only have constructor setters

Original comment by chengt on 28 Mar 2011 at 9:44

GoogleCodeExporter commented 9 years ago
sry I meant bump the priority

Original comment by chengt on 28 Mar 2011 at 9:44

GoogleCodeExporter commented 9 years ago
Any update on this

Original comment by Wig...@gmail.com on 24 May 2011 at 3:14

GoogleCodeExporter commented 9 years ago
I've attached a patch to inject an association or collection as a constructor 
parameter. I've added tests to BindingTest to check that both the XML and 
Annotation-based code works. The maven build was successful.
Please make any adjustments that you feel are necessary.
It would be appreciated if this could be included in the next release as the 
code changes were relatively minor and shouldn't involve much risk.
Thanks.

Original comment by gus4...@gmail.com on 12 Jun 2011 at 1:48

Attachments:

GoogleCodeExporter commented 9 years ago
Hi, is there any update on this? The patch was uploaded 2 weeks ago and we were 
hoping to see if the changes could be integrated. Thanks.

Original comment by gus4...@gmail.com on 27 Jun 2011 at 6:27

GoogleCodeExporter commented 9 years ago
It looks like a reasonable change.   When I originally wrote MyBatis 3, I 
didn't do anything that should threaten this feature, I just didn't have time 
to test it (the implementation is relatively simple, but the edge cases and 
broad impacts are hard to quantify and test).  I can have a look at this one 
for you.

Original comment by clinton....@gmail.com on 27 Jun 2011 at 6:30

GoogleCodeExporter commented 9 years ago
Sure, thanks for that. I'll write a couple more tests and upload them. Let me 
know if there's any corner cases that you feel might be important.

Original comment by gus4...@gmail.com on 27 Jun 2011 at 7:03

GoogleCodeExporter commented 9 years ago
Hi Clint, just wondering if you've had a chance to take a look at the patch for 
this fix? Thanks.

Original comment by gus4...@gmail.com on 18 Jul 2011 at 11:17

GoogleCodeExporter commented 9 years ago
Hey Gus.
   I looked at your patch briefly and may have missed something but it seems that it potentially suffers from the n+1 as it only allows for an additional select. It's a good patch but I feel like it's only half the functionality. I don't know if you had plans or already done any work on including the rest of the functionality.

Original comment by chengt on 21 Jul 2011 at 2:46

GoogleCodeExporter commented 9 years ago
I updated your patch to build against trunk (3.0.6-SNAPSHOT) of mybatis and 
added resultmap handling.
I added a small test for it that uses a mix of what you added with the select 
and what I added with resultmap handling. It's kind of a naive test but it's 
getting late over here.

Original comment by chengt on 21 Jul 2011 at 8:18

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for that! Apologies, I hadn't thought about that enough and had ignored 
half of the question. Really appreciate that you've taken a look over it and 
hope it might show up in a release soon.

Original comment by gus4...@gmail.com on 21 Jul 2011 at 1:24

GoogleCodeExporter commented 9 years ago
Fix provided by Gus and Tim.  Committed as rev. 3833

Original comment by clinton....@gmail.com on 22 Jul 2011 at 9:39

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 25 Jul 2011 at 8:26

GoogleCodeExporter commented 9 years ago
Is it possible that I can use a nested resultmap instead of an Inner 
select(<arg column="id" javaType="java.util.List" 
select="selectPostsForBlog"/>)? I've read that inner selects perform far worse 
than a join. But I have to test this, though. It would be great to use the same 
syntax like usual resultmaps with the collection-tag between the arg-tag.

Any ideas?

Original comment by m...@philippadam.com on 4 Aug 2011 at 11:33

GoogleCodeExporter commented 9 years ago
I didn't try it with collections but the patch that was applied has both the 
select feature that Gus submitted the patch for as well as the resultMap 
feature.

So if your question was if this would allow us to do :

<arg column="id" javaType="java.util.List" resultMap="postsResultMap"/>

Then yes that will work now. I've been using an internal release of the 3.0.6 
in my current projects using this feature to handle immutable datamodels that 
don't expose a default constructor.

Original comment by chengt on 4 Aug 2011 at 2:08

GoogleCodeExporter commented 9 years ago
Thank you very much for your quick answer! That's actually what I searched for. 
I don't get it to work but I use MyBatis for three days now. That might be the 
reason. It would be great to see a full and simple example how to handle 
collections in a constructor.

Original comment by m...@philippadam.com on 5 Aug 2011 at 6:33