Share entity between resultMaps

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Share entity between resultMaps

Aleksandr Savvopulo
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?

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/a21f5ef1-fd7d-482d-8a1c-8153e016bb08n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Share entity between resultMaps

Guy Rouillier-2
"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

On 7/19/2020 10:30:25 AM, "Aleksandr Savvopulo" <[hidden email]> wrote:

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?

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/a21f5ef1-fd7d-482d-8a1c-8153e016bb08n%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emb9e0f6ea-b72f-4df6-a3b5-ef86d2736fbc%40asus.