ORM vs persistence framework
Hibernate is an object relations mapping (ORM) structure that maps Java classes to database tables. MyBatis is a persistence system, not an ORM. It maps SQL statements to Java methods.
Database schema
Hibernate can create a database schema according to your Java model, while MyBatis does not have such a function. Related discussions:
- Can MyBatis create a database schema?
Cache
Hibernate has a first level cache that cannot be disabled. This means that if you request an element through ORM and then delete it directly using SQL, it remains in the cache. You can explicitly clear the cache to get the latest results from the database. Related discussions:
- Do Jpa & Hibernation load data that change asynchronously in the database?
- What is caching of the first and second levels in sleep mode?
Optimistic lock management
There are also differences for optimistic locking management:
MyBatis does not support optimistic concurrency control natively, unlike ORM tools like Hibernate / JPA with @Version annotation.
Related discussions:
lazy loading
Hibernate will attempt to load the entire graphic, except for the objects marked for lazy loading. myBatis will load the data according to the SQL query. Lazy loading can improve performance, but can lead to connection leaks if used with <property name="hibernate.enable_lazy_load_no_trans" value="true" /> properties. Related discussions:
- org.hibernate.LazyInitializationException - failed to initialize proxy - no session
- Solve the Hibernate Lazy-Init problem with hibernate.enable_lazy_load_no_trans
Hibernate session management
Object operations, such as saving, updating, or deleting, are performed using the Hibernate Session . This requires a good understanding of how to implement the right Hibernate session management strategy to avoid detached entity passed to persist and other Hibernate related phenomena.
Sometimes it may take more time trying to understand the basic behavior of Hibernate than adding a bit more work and writing raw SQL statements for myBatis.
Cascading
Hibernate provides cascading, removing orphans, and other functions for graphs of objects until they are present in myBatis - to implement them you need to explicitly write SQL queries.
Inquiries
In myBatis you will write almost raw SQL queries. Hibernate has several options for generating a query: SQL, HQL, API criteria. Sometimes it may be advisable to use the criteria API when you have many optional fields in the criteria. This will provide a more structured approach to the formation of the request and, possibly, will avoid related errors.