I've done some research on my own and understand the basic concept. But some insights can only be gained through actual experience.

What are the advantages of myBatis that would make it worth learning a new framework?
In what case would you avoid using it?

  • 3
    I tested both and ended up using Cayenne.
    – deadalnix
    Jul 26, 2012 at 1:38

5 Answers 5


Consider what you're trying to achieve. Typically, the Command Query Response Segregation model works well for complex domains.

The reason is that you're trying to do one of two things typically:

  1. Create/Update/Delete some complex domain entities
  2. Run analytic fetch queries (i.e. summation/aggregation queries)

Hibernate works well for case 1 allowing you to just make a POJO and persist/update it. It also does this quickly, unless your domain is quite large.

myBatis is great for fetch queries (case 2) where you just want an answer. Hibernate would attempt to load the entire object graph and you'd need to start tuning queries with LazyLoading tricks to keep it working on a large domain. This is important when running complex analytic queries that don't even return entity objects. Hibernate only offers SqlQuery and bean Transformers in this case with huge default types like BigDecimal, while myBatis could easily map to a simple POJO non-entity.

These two cases are the difference between Commands where you want to change the domain data and Responses where you just want to fetch some data.

So, consider these two cases and what your application does. If you have a simple domain and just fetch information, use myBatis. If you have a complex domain and persist entities, use Hibernate. If you do both, consider a hybrid approach. That's what we use on our project that has thousands of entities to keep it under control. ;)

  • 2
    Hibernate works well with your case 2 as well. For a simple example, see: mkyong.com/hibernate/hibernate-native-sql-queries-examples Mar 26, 2013 at 16:40
  • 3
    "Hibernate would attempt to load the entire object graph" this sounds like a configuration mistake. "Need to start tuning queries with LazyLoading tricks" sounds like you have set fetch=eager somewhere. JPA has JPQL which is practically SQL, yet well integrated with Hibernate/JPA. I think it does #2 very well. It's common for people to run in production for a year or more before having an ORM configured 100% correctly. I'm not minimizing when I say that it's a configuration error - configuration with these tools can be really time consuming and tricky! Nov 1, 2013 at 16:52
  • 3
    @GlenPeterson I'm not saying Hibernate cannot be tamed, but for most people, out of the box, complex domain entities will be loaded slowly unless optimizations are made, as you quoted. Complex analytic queries are more straightforward with myBatis for most users, though the same can indeed be achieved with an ORM. The question for many is whether they need that ORM's vendor independence for their product. Nov 1, 2013 at 17:16
  • 12
    Hibernate kind of pretends there is no DB underneath and you just treat you data model as if it was designed using Java collections. Which is very often not the case. On DB level data representation may be very different and trying to map it automatically into Java collections does not necessarily make any sense. This is why Hibernate is a perfect example of "Leaky abstraction". MyBatis is much simpler, easy to comprehend, there is no magic and.. yes, there is SQL which I find a good thing because there is nothing wrong with using SQL for SQL-based databases.
    – Marcin
    Mar 16, 2014 at 22:39
  • 2
    @Marcin great point. I've seen too many problems with collections emulation in Hibernate. For example some code calls myBag.size() and Hibernate tries to load 750K objects into memory before counting them. If you only need SQL, use SQL. Mar 17, 2014 at 2:01

MyBatis is SQL centric. It heps you calling SQL statements and mapping results (tables) to object trees.

The main benefit is that it is not an ORM. It does not map tables to object so does not suffer the orm impedance mismatch. Fits well for complex or legacy databases or to use db features like stored procedures, views and so.

It is quite simple and easy to learn so fits also well for low skilled teams because there is no need to have an hibernate guru among them.

Have a look at jpetstore 6 http://mybatis.org/spring/sample.html

  • 2
    Those "low skilled teams" still need to know SQL.
    – perp
    Nov 15, 2012 at 16:49
  • Good concise answer, the two main differences are those: db centric + shallow learning curve. But I don't totally agree with "Fits well for complex or legacy databases". The advantage of being db centric becomes more apparent when you have more control over the db design.
    – DPM
    Aug 9, 2013 at 14:50

Since the question refers to my comment, here's what I had in mind writing it.

First of all, it is derived from the context of your original question. In other circumstances I could give a different advice. The point that made me suggest MyBatis is this:

...we encountered some performance problems.

We decided to drop hibernate in favor of plain Jdbc to gain database performance...

In one of past projects, our team has been considering moving from Hibernate for the reasons like you describe. Similar to you, we were going to switch to JDBC, but colleagues from another project recommended us MyBatis. Team decided to give it a try, while keeping JDBC as a fallback option in the case if things go wrong.

At that moment, I knew nothing about MyBatis but had enough experience with JDBC to be sure it will do the job. Despite this, I had been strongly supporting idea of trying MyBatis, main reason being that per my past experience, amount of boilerplate code we would have to write with JDBC would be just daunting.

  • To be fair, I like JDBC for it being simple to understand, reliable and for giving good feel of control over database interaction, but the price one pays for it is really high. My fingers start aching every time I recall how much boilerplate I had to type with JDBC.

Anyway, we tried MyBatis and it worked as advertised. That's why I wrote the comment you ask about.

In case if you expect me to give a detailed overview of the technology, or somehow praise its superiority - sorry I can't do that. If I could - I'd already write that in a separate answer to your original question, instead of giving short comment. I mentioned I knew nothing about MyBatis back then - well I still have quite little knowledge of it sorry. Transition from Hibernate was done by other team members and it did not impact the code I've been working on. I only recalled key takeaways (based on which I made my comment), namely that 1) MyBatis resolved the issues we had with Hibernate, 2) it did not introduce issues of its own and 3) it allowed us to avoid writing boilerplate code I was expecting in case if we switch to JDBC. That's all.


Hibernate is well know for too much magic, unexpected behaviour and big learning curve. There are other frameworks out there which are more focused on simplicity and will let you be in control.

myBatis is one of them, my project MentaBean is another one. I've written a blog post about it that might help.

  • 6
    Hi Sergio and welcome. Our community generally frowns upon overt self promotion, and we require you to clearly disclose your affiliation in your answers. I've edited your answer to point out that MentaBean is a project you're involved with, and that you've written the blog post you point to, if you don't like the phrasing feel free to change it, but please maintain some sorts of disclosure that you're involved with MentaBean. Read the relevant section in our FAQ for more details.
    – yannis
    Nov 15, 2012 at 9:24
  • Hi Yannis. I will do that. Thanks for the heads up. :) Nov 15, 2012 at 23:23

I used Hibernate for a data loading and data transformation project 5 years ago using Hibernate 3 and I thought it was wonderful. I am doing a small e-commerce application and I tried using Hibernate 4 and I was extremely disappointed. They have removed the tools and tightly integrated to the IDE. I tried MyBatis and I go everything working in a single night and I am very please with how easy it is to integrate into an application. I think Hibernate has become too bloated and I would use EJB 3 over Hibernate at this point.

  • 1
    How does EJB 3 and Hibernate compare???
    – Jose
    Mar 11, 2014 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.