user10759930
user10759930

Reputation:

Nested mapping of object using annotations in Mybatis

I have a POJO like this

 public class Application
{
  private String dcn;
  private Party applicant;
  private Date createdDateTime;
  private NamedValue[] flags;
  // containing getter and setters
  }
public class Party
{
  private String lastName;
  private String rfc;
  // containing getter and setters..
  }

And my table columns are..

dcn,applicantLastName,applicantRFC,createdDateTime,flag

all are varchar type. I am using annotations to get data from the above table and set it to POJO using below Query.

@Select("SELECT dcn,applicantLastName,applicantRFC,createdDateTime,flag FROM DnA_Application WHERE dcn=#{dcn}")
  List<Application> getByProc(int proc);

I am not getting data for Party bean from table using above config .Please help.

Upvotes: 1

Views: 5907

Answers (1)

There's no way to map the result of the join using annotations. This is not supported in mybatis. Here's a quotation from the documentation:

You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references

One way to do mapping is to define a result map in the xml and reference it via @ResultMap.

mapper.xml:

<resultMap id="ApplicationResultMap" type="Application" autoMapping="true">
   <id property="dcn" column="dcn"  />
   <association property="party" javaType="Party" resultMap="PartyResultMap" columnPrefix="applicant"/>
</resultMap>

In mapper interface:

@Select("SELECT dcn,applicantLastName,applicantRFC,createdDateTime,flag FROM DnA_Application WHERE dcn=#{dcn}")
@ResultMap("ApplicationResultMap")
List<Application> getByProc(int proc);

Upvotes: 3

Related Questions