Nested object mapping

23 views
Skip to first unread message

Максим Касьянов

unread,
Jan 18, 2019, 12:40:40 PM1/18/19
to mybatis-user
Hi,

I have annotation base myBatis configuration.
How i can map nested object with annotaion?
Thank you in advance.

For instance:

class Example{


 
public String field1;
 
public NestedObject nestedObect;
 
}


class NestedObject{


 
public String field2;
 
}


class ExampleMapper{


 
@Select("select 'field1' as field1, 'field2' as nestedObjectField")
 
Example map();


}

Guy Rouillier

unread,
Jan 19, 2019, 1:29:16 AM1/19/19
to mybati...@googlegroups.com
Try nestedObject.field2.

--
Guy Rouillier
--
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 mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David

unread,
May 11, 2019, 12:58:33 AM5/11/19
to mybatis-user
I suppose that in your data model the entities Example and NestedObject have a relation of one to one, the solution that I recommend is:

@Select("select field1 from ExampleEntity")
@Results(id = "selectMap", value = {
@Result(property = "field1", column = "FIELD1", jdbcType = JdbcType.VARCHAR),
        @Result(property = "nestedObject", javaType=NestedObject.class, column = "ID_NESTEDOBJECT", one=@One(select="getNestedObjectById"))
})
Example map();

@Select("select field2 from NestedObjectEntity where ID_NESTEDOBJECT=#{id}")
@Results(id = "nestedObjectMap", value = {
@Result(property = "field2", column = "FIELD2", jdbcType = JdbcType.VARCHAR)
})
NestedObject getNestedObjectById(@Param("id") Long id);
Reply all
Reply to author
Forward
0 new messages