Hibernate Vs iBATIS

For our new product reorganization, we are in the process of selecting the best frameworks from Java. Since the consideration is to switch to an agnostic database approach for the model, we are working on options between Struts + Spring with iBATIS or Hibernate. Please advice, which is better, since both offer perseverance.

+58
java frameworks hibernate ibatis persistence
Dec 31 '09 at 8:39
source share
6 answers

Ibatis and Hibernate are completely different animals.

As I usually look at this: Hibernate works better if your view is more object oriented. If, however, you are more database oriented, then Ibatis is a much stronger choice.

If you have full control over your circuit and you do not have extremely high bandwidth, then Hibernate can work quite well. The object model makes quite convenient code, but with enormous complexity.

If you are dealing with an "outdated" database schema where you need to write fairly complex SQL queries, the likelihood that Ibatis will work better.

HQL (Hibernate Query Language) is another language that you will need to learn, and even then you will probably find cases where you still need to write SQL. What else, most likely, you will spend half a day trying to figure out the right combination of XML, properties, annotations, etc., to get Hibernate to generate an executable SQL query.

There is no universal answer "A is better than B" for this question.

+159
Dec 31 '09 at 8:51
source share

Think about what you are trying to achieve. Typically, segmentation of the response to a command request model works well for complex domains.

The reason is because you are trying to do one of two things:

  • Create / update / delete some complex domain objects
  • Running analytic query queries (i.e. summation / aggregation queries)

Hibernate works great for case 1, letting you just do a POJO and save / update it. It also does this quickly if your domain is quite large.

myBatis is great for sample queries (case 2), where you just want to get an answer. Hibernate will try to load the entire graphic object, and you will need to start customizing your queries using LazyLoading tricks to make it work on a large domain. Conversely, if you just need some kind of analytic POJO page, implementing myBatis of the same request will be trivial.

Because of this, myBatis is faster than Hibernate in SELECTS.

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

So, consider these two cases and what your application does. If you have a simple domain and just select the information, use myBatis. If you have a complex domain and persistent objects, use Hibernate. If you do both, consider a hybrid approach. This is what we use in our project, in which thousands of organizations keep it under control .;)

+38
Mar 26 '13 at 15:11
source share

Cletus did a great job summarizing this comparison. Hibernate works well when you are managing a data model that is more object oriented, while iBATIS works well when you need to integrate with an existing database and be more data oriented.

I also think that Hibernate has a little more learning curve. With iBATIS, it’s pretty easy to understand what’s going on, while with Hibernate there’s a “magic”. In other words, beginners may find iBatis easier to use and understand.

But I'm not saying that you should prefer iBatis, iBatis and Hibernate are just different, as mentioned above.

And by the way, if you are going to Hibernate, perhaps consider using the standardized JPA and EJB 3.0 (JSR-220) object-relational mapping annotations provided by Hibernate Annotations .

+15
Dec 31 '09 at 10:47
source share

if you are already using Spring, I would start with Spring JDBC and not dive right into Hibernate or iBatis. If you write your level of resilience in terms of interfaces, you should not have problems with the implementation of the switch after you get Hibernate or iBatis under your belt.

There is no reason why this should be an all-or-nothing solution. Use what works best for your situation.

+4
Dec 31 '09 at 13:51
source share

Hibernate is an ORM, that is (at the most basic level), it maps instances of Java objects to real rows in the database table. As a rule, for pojo obtained through Hibernate: any manipulations and modifications of this pojo will be displayed in the database. Hibernate will generate and execute the appropriate SQL at the appropriate time.

Mybatis (at the most basic level) is just a tool for building and executing SQL, which is stored in xml files. It does not map instances of Java objects to rows in the database table, but rather maps Java methods to SQL queries, and therefore this is not ORM. It can also return pojo, but they are not tied to any persistence context.

Both tools do a lot more than described above, but one is ORM and the other is not.

The criteria that allow you to choose which one to use, I believe, depend to a decisive extent on the database model with which you should work.

For example, imagine a large stretch pattern representing some kind of insurance model. Developers need to get data and interact with this data in a way that fits the business.

The developer will come to work and will never expect to have the necessary business knowledge to write all sql manually (which will require Mibatis). Sleep mode is suitable for this scenario.

Business analysts define data, entities, relationships and interactions, as well as their experience. The Java developer then uses Hibernate to "walk around the model." A business developer can become very productive quickly, without having to write a complex error prone to sql to work on a very complex scheme.

In my experience, both Hibernate and Mybatis are used regularly in the same project.

Where is hibernate used for

  • General CRUD Functionality
  • "Walk" of the relational model "domain object"
  • Session Management

and where is mybatis used to

  • special requests
  • run (and interact) stored procedures
  • support for very specific or complex queries
  • support complex search queries where search criteria are dynamic and search queries
+4
Feb 16 '16 at 12:12
source share

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.

+4
Mar 02 '16 at 8:42 on
source share



All Articles