New object instance for each row in collection wanted

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

New object instance for each row in collection wanted

nilsw
This collection from a result map:

<collection property="eventParticipants" javaType="ArrayList"
column="{objectId=objectId,hId=hId}" ofType="Participant"
select="getPhotoCreationMotifParticipant"/>

Uses this nippet of long ugly select:

select -- Depicted persons
   'Actor' as entityClass,
   #{objectId} as objectId,
   #{hId} as hId,
   'no.anastazia.domain.role.media-depicted-actor' as roleId,
   to_char(mp.jpnr) as entityId
from
   motiv_person mp
where
   mp.objid = #{objectId}
   and mp.objid_siste = #{objectId}
   and mp.rolleid = 70

which in one particular case gives three result rows, with different
values in the last column entityId, all other columns being the same
for all rows.

These result maps:

<resultMap id="photoCreationParticipantResult" type="Participant">
   <result property="roleId" column="roleId"/>
   <discriminator javaType="String" column="entityClass">
      <case value="Motif"
resultMap="photoCreationParticipantMotifResult"/>
      <case value="Actor"
resultMap="photoCreationParticipantActorResult"/>
      <case value="Place"
resultMap="photoCreationParticipantPlaceResult"/>
   </discriminator>
</resultMap>

<resultMap id="photoCreationParticipantActorResult" type="Participant"
extends="photoCreationParticipantResult">
   <association property="entity" javaType="Actor">
      <constructor>
         <idArg column="entityId" javaType="String"/>
      </constructor>
      <collection property="title" javaType="HashSet"
column="entityId" ofType="Title" select="getAuthorName"/>
   </association>
</resultMap>

sort of work, but I don't get three Participant objects in the
resulting ArrayList but one. It is created for the first row, for the
remaining rows, the Participant object's entity field is updated with
the Actor object created by the last resultMap.

What can I do to get a new Participant object for each row in the
result?
Reply | Threaded
Open this post in threaded view
|

Re: New object instance for each row in collection wanted

Clinton Begin
Administrator
Hmm... sorry, this is not optional.  It was done intentionally to hopefully avoid a horrible memory consequence.  

That said... you should probably try not write code that depends specifically on instance identity.

Clinton

On Mon, Jan 10, 2011 at 3:26 PM, nilsw <[hidden email]> wrote:
This collection from a result map:

<collection property="eventParticipants" javaType="ArrayList"
column="{objectId=objectId,hId=hId}" ofType="Participant"
select="getPhotoCreationMotifParticipant"/>

Uses this nippet of long ugly select:

select -- Depicted persons
  'Actor' as entityClass,
  #{objectId} as objectId,
  #{hId} as hId,
  'no.anastazia.domain.role.media-depicted-actor' as roleId,
  to_char(mp.jpnr) as entityId
from
  motiv_person mp
where
  mp.objid = #{objectId}
  and mp.objid_siste = #{objectId}
  and mp.rolleid = 70

which in one particular case gives three result rows, with different
values in the last column entityId, all other columns being the same
for all rows.

These result maps:

<resultMap id="photoCreationParticipantResult" type="Participant">
  <result property="roleId" column="roleId"/>
  <discriminator javaType="String" column="entityClass">
     <case value="Motif"
resultMap="photoCreationParticipantMotifResult"/>
     <case value="Actor"
resultMap="photoCreationParticipantActorResult"/>
     <case value="Place"
resultMap="photoCreationParticipantPlaceResult"/>
  </discriminator>
</resultMap>

<resultMap id="photoCreationParticipantActorResult" type="Participant"
extends="photoCreationParticipantResult">
  <association property="entity" javaType="Actor">
     <constructor>
        <idArg column="entityId" javaType="String"/>
     </constructor>
     <collection property="title" javaType="HashSet"
column="entityId" ofType="Title" select="getAuthorName"/>
  </association>
</resultMap>

sort of work, but I don't get three Participant objects in the
resulting ArrayList but one. It is created for the first row, for the
remaining rows, the Participant object's entity field is updated with
the Actor object created by the last resultMap.

What can I do to get a new Participant object for each row in the
result?