Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question originates from a comment I received on one of my questions -

I'd rather drop hibernate in favor of mybatis instead of jdbc

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?

share|improve this question
1  
I tested both and ended up using Cayenne. – deadalnix Jul 26 '12 at 1:38

5 Answers

up vote 2 down vote accepted

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. ;)

share|improve this answer
1  
Hibernate works well with your case 2 as well. For a simple example, see: mkyong.com/hibernate/hibernate-native-sql-queries-examples – Mike Partridge Mar 26 at 16:40
@MikePartridge I meant non-entity analytic queries. In this case you must use the Tranformers to map to beans, but these assume the worst case types like BigDecimal and BigInteger which are far from optimal. myBatis is far easier to map in that case to the target types. – Joseph Lust Mar 26 at 22:02

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

share|improve this answer
Those "low skilled teams" still need to know SQL. – perp Nov 15 '12 at 16:49

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.

share|improve this answer
2  
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 Rizos Nov 15 '12 at 9:24
Hi Yannis. I will do that. Thanks for the heads up. :) – Sergio Oliveira Jr. Nov 15 '12 at 23:23

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.

share|improve this answer

I was using Hibernate for a few projects and in some cases, Hibernate does not handle well some complicated SQL. That's the reason why I created this open source project Stardust. It combines the power of JPA annotations to quickly map database metadata to Java model objects, and the flexibility of SQL. If you are very familiar with SQL, this may be a good option to look at.

I'm creating more examples for this projects, but you can view some from the wiki.

share|improve this answer
Stardust looks interesting. Welcome to the site. – Kshitiz Sharma Jul 25 '12 at 6:35
-1 for not answering the question. – Mike Partridge Mar 26 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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