Hibernate / MyBatis Interview questions
What is a resultMap?
A resultMap is an explicit, named mapping definition in a Mapper XML file that describes how the columns of a query's result set correspond to the properties of a Java object, used whenever the automatic, name-matching mapping behind a simple resultType isn't sufficient.
<resultMap id="orderResultMap" type="com.example.model.Order"> <id property="orderId" column="order_id"/> <result property="orderDate" column="order_date"/> <association property="customer" javaType="com.example.model.Customer"> <id property="customerId" column="customer_id"/> <result property="name" column="customer_name"/> </association> </resultMap>
Beyond simple column-to-property mapping, a resultMap supports nested structures via <association> (for a single related object, as shown above) and <collection> (for a list of related objects), letting a single query's flattened result set be reassembled into a properly nested Java object graph.
Once defined, a resultMap can be reused across multiple statements by referencing its id in each statement's resultMap attribute, avoiding the need to redefine the same mapping structure repeatedly for every query that returns the same shaped result.
More Related questions...