Hibernate / MyBatis Interview questions
How do you troubleshoot a MyBatis mapping exception?
MyBatis mapping errors generally fall into a handful of recurring categories, and working through them in a consistent order — from configuration issues to type mismatches — is usually faster than guessing at the root cause from the exception message alone.
- Check the exception's root cause, not just the top-level message: MyBatis often wraps a lower-level JDBC or reflection exception, and the actual useful detail is usually in the cause chain.
- Verify namespace and id correspondence: a mismatched mapper namespace or a statement id that doesn't exactly match the interface method name produces a "mapped statement not found" error.
- Check for a BindingException (missing parameter): often caused by referencing a #{} placeholder that doesn't match an actual parameter name or object property, especially with multiple unnamed parameters missing @Param.
- Verify resultMap/resultType field alignment: unmapped columns or mismatched types commonly cause silent null fields or a type conversion exception, rather than an obvious hard failure.
- Check for duplicate mapped statement ids across mapper files, which MyBatis will reject at startup rather than at query time.
Enabling MyBatis's SQL logging (via a logging framework configuration, showing the actual executed SQL and bound parameters) is often the single most useful diagnostic step, since it reveals exactly what MyBatis is sending to the database — frequently exposing that the real problem is a subtly wrong SQL statement or misbound parameter rather than a deeper configuration issue.
More Related questions...