This is my first question here, so please let me know if there's anything I can provide to make my question clearer. Thanks!
Is there anyway to tweak MyBatis (3.0.4) so that when it's running through the nested result maps and creating objects based on the results, it doesn't create "duplicate" objects and instead references the object that has already been created? To clarify, let's say I've got an object that consists of the following (simplified) info:
Public class PrimaryObject {
Private Contact role1;
Private Contact role2;
Private List<OtherObject> otherObjects;
}
Public class OtherObject {
Private String otherProperty;
Private Contact otherRole;
}
And the result maps/SQL (assume type aliases):
<mapper namespace="PrimaryObject">
<resultMap id="primaryObject" type="primaryObject">
<association property="role1" column="ROLE_1_ID"
select="Contact.findContactByPK" />
<association property="role2" column="ROLE_2_ID"
select="Contact.findContactByPK" />
<collection property="otherObjects" column="PRIMARY_OBJECT_ID"
javaType="List" ofType="OtherObject"
select="OtherObject.findOtherObjectsByPrimaryObjectPK" />
</resultMap>
<select id="selectPrimaryObjectByPK" parameterType="..."
resultMap="primaryObject">
SELECT * FROM PRIMARY_OBJECT_TABLE
WHERE PRIMARY_OBJECT_ID = #{value}
</select>
...
<mapper namespace="OtherObject">
<resultMap id="otherObject" type="otherObject">
<result property="otherProperty" column="OTHER_PROPERTY" />
<association property="otherRole" column="OTHER_ROLE_ID"
select="Contact.findContactByPK" />
</resultMap>
<select id="findOtherObjectsByPrimaryObjectPK" parameterType="..."
resultMap="otherObject">
SELECT OTHER_OBJECT.OTHER_PROPERTY, OTHER_OBJECT.OTHER_ROLE_ID
FROM OTHER_OBJECT JOIN PRIMARY_OBJECT
ON PRIMARY_OBJECT.PRIMARY_OBJECT_ID = OTHER_OBJECT.PRIMARY_OBJECT_ID
</select>
...
<mapper namespace="Contact">
<resultMap id="contact" type="Contact">
<result property...
...
</resultMap>
<select id="findContactByPK" parameterType="..." resultMap="contact">
SELECT * FROM CONTACT...
</select>
Now let's say a contact is acting as both role1
and role2
on primaryObject
. From what I've seen, MyBatis creates the contact object referenced in role1
, and when it hits role2
, it simply references the object it created for role1
. That's great!
However, let's say that same contact is acting as the otherRole
in otherObject
. MyBatis now creates an identical contact object, as opposed to just referencing the original contact
created/referenced when making the primaryObject
. Is there any way I can prevent this from happening, and instead just store a reference in my otherObject
that references that same contact
that the two different fields in primaryObject
are pointing to? I've been looking at a custom ResultHandler, but the example's complexity coupled with my lack of experience is preventing me from understanding if it even addresses the problem I'm trying to solve in the first place. Thanks in advance for your help and patience!