I'm trying out MyBatis in a project and so far have been fairly impressed. I'm using the Spring Boot starter, and ideally would like to have as much config as possible in annotations.
Everything was going well until I tried to implement a 'has-a' join in my mapper. I have a customer entity which has an address:
Customer (id, name, email, ...)
Address (id, customerId, street, city, ...)
<resultMap id="customer" type="foo.domain.Customer">
<id property="id" column="customerId"/>
<association property="address" resultMap="address"/>
<resultMap id="address" type="foo.domain.Address">
<id property="id" column="addressId"/>
@Select("SELECT * FROM Customer cus LEFT OUTER JOIN Address adr ON adr.customerId = cus.customerId")
I think I've found the answer to my own question here.
There are three auto-mapping levels:
NONE - disables auto-mapping. Only manually mapped properties will be set.
PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
FULL - auto-maps everything.
The default value is PARTIAL, and it is so for a reason. When FULL is used auto-mapping will be performed when processing join results and joins retrieve data of several different entities in the same row hence this may result in undesired mappings.
So by default, in my case, auto-mapping does not happen. I need to decide whether to switch it on or not.