具有相同基础映射器的多个关联

Multiple assocations with same base mapper

我有一个版本信息的基本 resultMap 定义如下:

   <resultMap id="VersionedEntityMapper" type="org.chu.wit4h.db.AbstractVersionedEntity">
      <result property="version" column="version"/>
      <result property="lastModified" column="lastModified"/>
      <result property="lastModifier" column="lastModifier"/>
   </resultMap>

然后是一些扩展它的实体映射器:

   <resultMap id="Entity1Mapper" type="org.Entity1" extends="VersionedEntityMapper">
      <result property="name" column="name" />
      <result property="firstName" column="firstName" />
   </resultMap>

   <resultMap id="Entity2Mapper" type="org.Entity2" extends="VersionedEntityMapper">
      <result property="room" column="room"/>
   </resultMap>

一切正常,直到我这样做:

   <resultMap id="Entity3Mapper" type="org.Entity3">
      <id property="id" column="id" />
      <association property="info" javaType="org.Entity1" resultMap="Entity1Mapper"/>
      <association property="location" javaType="org.Entity2" resultMap="Entity2Mapper"/>
   </resultMap>

因此,列名版本、lastModified 和lastModifier 冲突。 我可以在 SQL 语句中为它们添加别名,但如何将前缀传递给内部映射器?

为了使用现有的 resultMap 映射关联实体,您需要向结果中关联实体的所有列添加一些前缀,并通过 columnPrefix 属性指定该前缀。

在上面的示例中,Entity1 的字段在 select Entity3.

的查询中应命名为 entity1_name, entity1_first_name, entity1_version

应修改映射:

<resultMap id="Entity3Mapper" type="org.Entity3">
   <id property="id" column="id" />
   <association property="info" javaType="org.Entity1" resultMap="Entity1Mapper"
       columnPrefix="entity1"/>
   ...
</resultMap>