emacarron / old-google-code-issues

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

constructors with associations and collections #480

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
MyBatis 3.0.6 java

Please describe the problem.  Unit tests are best!
It is not possible to create immutable domain objects i.e. through constructor 
initialization the same way you can create mutable objects i.e. through setters 
when a query includes associations and/or collections.

What is the expected output? What do you see instead?
To create truly immutable domain objects on should be able to turn this

<resultMap id="objectBuilderResultMap" type="ObjectBuilder">
 <id property="id" column="id" />
 <association property="o1" resultMap="o1ResultMap"/>
 <collection property="o2List" ofType="Object2" resultMap="o2ResultMap"/>
</resultMap>

into this:

<resultMap id="domainObjectResultMap" type="DomainObject">
 <idArg column="id" javaType="Long" />
 <arg resultMap="o1ResultMap" javaType="Object1" />
 <arg resultMap="o2ResultMap" javaType="List" />
</resultMap> 

Please provide any additional information below.
One workaround, which is clean but involves a lot of developer overhead is to 
create a builder object for each domain object, initialize the builder through 
its setter methods, then let the builder return an immutable domain object via 
a build() method for example.

Another workaround is to use the "select" attribute in the "arg" element:
 <arg resultMap="o1ResultMap" javaType="Object1" select="selectId" />

Original issue reported on code.google.com by jms.cer...@gmail.com on 24 Dec 2011 at 11:42

GoogleCodeExporter commented 9 years ago
I believe that this has already been resolved: 
http://code.google.com/p/mybatis/issues/detail?id=15

Original comment by gus4...@gmail.com on 6 Jan 2012 at 10:51

GoogleCodeExporter commented 9 years ago
Jorge, as gus400 pointed (he solved this issue) have a look at the "select" 
attribute.

Original comment by eduardo.macarron on 7 Jan 2012 at 9:49

GoogleCodeExporter commented 9 years ago
Hello Eduardo and gus400,

I don't think using the select attribute is addressing the issue, yes, it is 
one possible solution as I pointed out in the "Please provide any additional 
information below" section of this issue. By saying that one should use the 
select attribute in cases like this (immutable objects), isn't it the same as 
saying that one cannot use joins (associations/collections) in queries?

Apologies if I've missed something.
Jorge 

Original comment by jms.cer...@gmail.com on 7 Jan 2012 at 11:14

GoogleCodeExporter commented 9 years ago
Hi Jorge,
Apologies - I'm not sure if I quite understand what is necessary in this case.
I'll provide a couple of examples that are used in the tests - please let us 
know if they provide the functionality that you need:
    <resultMap id="blogUsingConstructor" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg column="author_id" javaType="domain.blog.Author"
                 select="org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"/>
            <arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
        </constructor>
    </resultMap>

    <resultMap id="blogUsingConstructorWithResultMap" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg javaType="domain.blog.Author" resultMap="org.apache.ibatis.binding.BoundAuthorMapper.authorResultMap"/>
            <arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
        </constructor>
    </resultMap>

If this does not provide the functionality that you need please let us know.
Gus.

Original comment by gus4...@gmail.com on 7 Jan 2012 at 1:22

GoogleCodeExporter commented 9 years ago
Hi gus400, thanks for the reply.

Well honestly, I can't say that it doesn't provide the desired end result. 
However, it does so at the cost of sending off an extra select which isn't 
necessary if I were using setter initialization.

** With setter initialization:

select 
  b.id as blog_id,
  b.title as blog_title,
  p.id as post_id,
  p.comment as post_comment
from blog b
inner join post p on b.id = p.blog_id
where b.id=100;

where the "collection" attribute will support the posts returned:
...
<collection property="posts" ofType="Post" resultMap="postResultMap"/>
...

** With constructor initialization I essentially have to do it two steps:

select 
  b.id as blog_id,
  b.title as blog_title
from blog b
where b.id=100;

select 
  p.id as post_id,
  p.comment as post_comment
from post
where p.blog_id=100;

In the testing and design phase I started off with mutable domain objects. When 
I started the implementation phase and changed to immutable objects I realized 
I needed to also change most of my queries. It felt like I was being punished 
for wanting to use immutable objects :-)
Also, using constructor initialization means that I cannot perform one-to-many 
joins.

Thanks again,
Jorge.

Original comment by jms.cer...@gmail.com on 7 Jan 2012 at 10:06

GoogleCodeExporter commented 9 years ago
Sorry, it took me a while to understand what you wanted, but I get it now! :)
I'll take a look into it and see if I can create a patch.
Eduardo/whoever - I think this needs to be a separate bug rather than merged 
into issue 15.
Gus

Original comment by gus4...@gmail.com on 8 Jan 2012 at 10:22

GoogleCodeExporter commented 9 years ago
Hi gus400,

If you require more info let me know.

Thanks,
Jorge

Original comment by jms.cer...@gmail.com on 9 Jan 2012 at 10:02

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I have added a failing test for this issue in r4668. But... I have no idea 
about how to fix it. :)

Original comment by eduardo.macarron on 6 Feb 2012 at 12:12

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm running into the same issue as Jorge.  We too were interested in having 
mybatis return immutable pojos and we'd prefer to use multiple joins to build 
out collections and associations as nested ResultMaps as opposed to having to 
make additional select calls which increases database twitter.  Is there a way 
to reopen this issue?

Original comment by Daniel.L...@gmail.com on 21 Mar 2013 at 11:13

GoogleCodeExporter commented 9 years ago
This issue follows at

https://github.com/mybatis/mybatis-3/issues/101

Original comment by eduardo.macarron on 22 Nov 2013 at 8:18