Hibernate / MyBatis Interview questions
What is the difference between association and collection in resultMap?
Both elements map a nested, related object within a parent resultMap, but they differ in cardinality — exactly matching the distinction between a many-to-one/one-to-one relationship and a one-to-many relationship.
| association | collection |
| Maps a single nested object. | Maps a list (or other collection) of nested objects. |
| Used for many-to-one or one-to-one relationships. | Used for one-to-many relationships. |
| Uses javaType to specify the target class. | Uses ofType to specify the element type within the collection. |
| Example: an Order's single Customer. | Example: an Order's list of OrderItems. |
A subtle but important attribute difference follows directly from this cardinality distinction: association uses javaType because it's describing the type of the single nested property itself, while collection uses ofType because the property's own type is already known to be some collection type (like List) — what needs specifying is the type of the elements within that collection, which javaType wouldn't correctly express.
More Related questions...