iBATIS, Hibernate, and JPA are three different mechanisms for persisting data in a relational database. Each has its own advantages and limitations. iBATIS does not provide a complete ORM solution, and does not provide any direct mapping of objects and relational models. However, iBATIS provides you with complete control over queries. Hibernate provides a complete ORM solution, but offers you no control over the queries. Hibernate is very popular and a large and active community provides support for new users. JPA also provides a complete ORM solution, and provides support for object-oriented programming features like inheritance and polymorphism, but its performance depends on the persistence provider.
The choice of a particular persistence mechanism is a matter of weighing all of the features discussed in the comparison section of this article. For most developers the decision will be made based on whether you require complete control over SQL for your application, need to auto-generate SQL, or just want an easy-to-program complete ORM solution.
Key Features:
- iBATIS
- Maps the ResultSet from JDBC API to your POJO Objets.
- Makes use of SQL which could be database dependent
- Works very well for stored procedures, works very well for reporting applications, etc
- Simpler, Faster development time
- Flexible
- Enables the data model and the object model to be independent of each other.
- Hibernate:
- Maps your Java POJO objects to the Database tables
- Makes use of HQL which is relatively independent of databases and it is easier to change db in Hibernate.
- If you are using stored procedures, well you can do it in Hibernate but it is little difficult in comparison of iBATIS
- Generates SQL for you which means you don't spend time on SQL
- Provides much more advance cache
- Highly scalable
- HQL also supports many advanced features of pagination and dynamic profiling that SQL has never supported.
- JPA:
- JPA uses metadata annotations and/or XML descriptor files to configure the mapping between Java objects in the application domain and tables in the relational database.
- Defines a SQL-like query language, JPQL (Java Persistence Query Language), which is different from EJB-QL (EJB Query Language), the language used by entity beans.
- Is the standard object-relational mapping and persistence management interface for the Java EE 5 platform
- Entity Class Annotation (@Entity, @Table, @Column, @NamedQuery)
- JPA also supports SQL through the createNativeQuery() method of the EntityManager.
- Hibernate is one of the most popular frameworks that implements JPA.
When TO/NOT TO Use:
- Use iBATIS if:
- You need complete control of the SQL or the SQL queries need to be fine-tuned.
- your environment is driven by relational data model.
- you have to work existing and complex schema's.
- Use Hibernate if:
- Your environment is driven by object model and wants generates SQL automatically.
- Not to use JPA :
- Caching, which is not clearly defined in JPA but is well supported by Hibernate.
- JPA is defined to work with relational databases only. If your persistence solution needs to be extended to other types of data stores, like XML databases, then JPA is not the answer to your persistence problem.
Persistence solutions compared
Features | iBATIS | Hibernate | JPA |
---|---|---|---|
Simplicity | Best | Good | Good |
Complete ORM solution | Average | Best | Best |
Adaptability to data model changes | Good | Average | Average |
Complexity | Best | Average | Average |
Dependence on SQL | Good | Average | Average |
Performance | Best | Best | N/A * |
Portability across different relational databases | Average | Best | N/A * |
Portability to non-Java platforms | Best | Good | Not Supported |
Community support and documentation | Average | Good | Good |