Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List<X> in mapper doesn't work when using resultMap with association #403

Closed
izeye opened this issue May 4, 2015 · 5 comments
Closed

List<X> in mapper doesn't work when using resultMap with association #403

izeye opened this issue May 4, 2015 · 5 comments

Comments

@izeye
Copy link

izeye commented May 4, 2015

List<X> in mapper has only one element when using resultMap with association.

I have a mapper interface as follows:

public interface PersonMapper {

    List<ComplexPerson> findAllComplexPersonsWithResultMap();

    List<ComplexPerson> findAllComplexPersonsWithResultMapAndAssociation();

}

and a mapper xml as follows:

    <resultMap id="complexPersonWithResultMap" type="ComplexPerson">
    </resultMap>
    <select id="findAllComplexPersonsWithResultMap" resultMap="complexPersonWithResultMap">
        SELECT * FROM person
    </select>

    <resultMap id="complexPersonWithResultMapAndAssociation" type="ComplexPerson">
        <association property="name" javaType="samples.springboot.domain.PersonName" autoMapping="true" />
    </resultMap>
    <select id="findAllComplexPersonsWithResultMapAndAssociation" resultMap="complexPersonWithResultMapAndAssociation">
        SELECT * FROM person
    </select>

Without association, it produces List<ComplexPerson> having correct rows,

but with association, it produces List<ComplexPerson> having only one row.

The following project is reproducing the problem:

https://github.com/izeye/samples-spring-boot-branches/tree/mybatis-lombok

@mches
Copy link

mches commented May 4, 2015

I think you may need to add one or more <id column=.../> inside your <resultMap/> or <association/> so that MyBatis knows which rows correspond to which object instances.

https://mybatis.github.io/mybatis-3/sqlmap-xml.html#Result_Maps

@izeye
Copy link
Author

izeye commented May 6, 2015

@mches Thanks! It works as expected.

I agree that there's no way to know an ID of a row for MyBatis unless I provide it by using id in resultMap.

But there's a case that rows don't have IDs.

Should it work without id, right?

@mches
Copy link

mches commented May 14, 2015

<result column="id"/> would work too. For nested result maps (i.e. result maps with an association or collection), MyBatis constructs a row key for each row. Without any <id/> or <result/> elements, the row keys are identical for every row (even with auto-mapping). The row key is used to as a key to map of nested result objects. An object is constructed upon encountering the first row and put into the map. Upon encountering additional rows, it finds the already constructed object and updates it rather than creating a new object.

@izeye
Copy link
Author

izeye commented May 15, 2015

@mches Thanks for the details.

I tried with <result> and it works only when the column is unique as you mentioned above.

But there are some entities having some associations but not having any unique column, right?

I don't know MyBatis' internals

but it would be nice if hashCode() is used as a key when a user doesn't provide a key explicitly.

Does it make sense?

@harawata
Copy link
Member

As explained in the doc, association works without id, but it is not recommended due to its inefficiency.

In your case, the name columns are in the same table, so just map them as nested properties.

<resultMap id="complexPersonWithResultMapHavingIdAndAssociation" type="ComplexPerson" autoMapping="true">
  <id column="id" property="id" />
  <result column="first_name" property="name.firstName" />
  <result column="last_name" property="name.lastName" />
</resultMap>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants