Share entity between resultMaps

24 views
Skip to first unread message

Aleksandr Savvopulo

unread,
Jul 19, 2020, 10:30:25 AM7/19/20
to mybatis-user
hello

Simplified situation is following: 
I have User, Project and Company entities. User has Projects. 
Both User and Project has relation to Company.

I have select like this: 
```
select ....
from user u
    left join project p on u.id = p.user_id
    inner join company c on u.company_id = c.id
        where u.id = #{id}
```

In my situation project and user belongs to the same company. 
And on the entity level I want to have relation to the same Company 
java object. 

But mybatis creates separate Company objects fro each project. 

I used result map like this: 
```
<resultMap id="companyResultMap" type="Company">
    <result property="name" column="name"/>
</resultMap>

<resultMap id="projectResultMap" type="Project">
    <result property="name" column="project_name"/>
    <association property="company" resultMap="companyResultMap" />
</resultMap>

<resultMap id="userResultMap" type="User">
    <association property="company" resultMap="companyResultMap" columnPrefix="c_"/>
    <collection property="projects" resultMap="projectResultMap" columnPrefix="p_"/>
</resultMap>
```

I wonder is there any way to share object within the session, take it from existing 
result set instead of using separate select?

Guy Rouillier

unread,
Jul 20, 2020, 2:02:38 AM7/20/20
to MyBatis User
"In my situation project and user belongs to the same company."

If you know that apriori, then don't associate both the user and the project to company.  Just associate the user.

--
Guy Rouillier
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/a21f5ef1-fd7d-482d-8a1c-8153e016bb08n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages