Hibernate / MyBatis Interview questions
What is the difference between MyBatis and JPA/Hibernate in terms of philosophy?
The core philosophical divide is where control over SQL generation sits: JPA/Hibernate treats the object model as the primary artifact and derives SQL from it automatically, while MyBatis treats SQL itself as the primary artifact and maps it explicitly to Java objects.
| MyBatis | JPA/Hibernate |
| "SQL-first": SQL is written explicitly, objects are a mapping target. | "Object-first": entities are defined, SQL is derived automatically. |
| No entity lifecycle management or automatic dirty checking. | Full entity lifecycle: managed, detached, transient states with dirty checking. |
| Query language is just SQL (plus dynamic SQL tags). | JPQL/HQL, an object-oriented query language translated to SQL. |
| Database portability requires care, since SQL is often database-specific. | Better database portability, since Hibernate's dialect abstracts differences. |
Neither philosophy is universally superior: MyBatis's explicit-SQL approach shines when queries are complex, performance-critical, or need database-specific tuning, while JPA/Hibernate's object-first approach shines for straightforward CRUD-heavy applications where minimizing boilerplate and maximizing database portability matter more than fine-grained SQL control.
More Related questions...