`
seasar
  • 浏览: 21409 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iBATIS, Hibernate, and JPA: Which is right for you

阅读更多

iBATIS, Hibernate, and JPA: Which is right for you?

Object-relational mapping solutions compared

Object-relational mapping in Java is a tricky business, and solutions like JDBC and entity beans have met with less than overwhelming enthusiasm. But a new generation of ORM solutions has since emerged. These tools allow for easier programming and a closer adherence to the ideals of object-oriented programming and multi-tiered architectural development. Learn how Hibernate, iBATIS, and the Java Persistence API compare based on factors such as query-language support, performance, and portability across different relational databases.

In this article we introduce and compare two of the most popular open source persistence frameworks, iBATIS and Hibernate. We also discuss the Java Persistence API (JPA). We introduce each solution and discuss its defining qualities, as well as its individual strengths and weaknesses in broad application scenarios. We then compare iBATIS, Hibernate, and JPA based on factors such as performance, portability, complexity, and adaptability to data model changes.

If you are a beginning Java programmer new to persistence concepts, reading this article will serve as a primer to the topic and to the most popular open source persistence solutions. If you are familiar with all three solutions and simply want a straightforward comparison, you will find it in the section "Comparing persistence technologies."

Understanding persistence

Persistence is an attribute of data that ensures that it is available even beyond the lifetime of an application. For an object-oriented language like Java, persistence ensures that the state of an object is accessible even after the application that created it has stopped executing.

There are different ways to achieve persistence. The traditional approach to the problem is to use file systems that store the necessary information in flat files. It is difficult to manage large amounts of data in this way because the data is spread across different files. Maintaining data consistency is also an issue with flat-file systems, because the same information may be replicated in various files. Searching for data in flat files is time-consuming, especially if those files are unsorted. Also, file systems provide limited support for concurrent access, as they do not ensure data integrity. For all these reasons, file systems are not considered a good data-storage solution when persistence is desired.

The most common approach today is to use databases that serve as repositories for large amounts of data. There are many types of databases: relational, hierarchical, network, object-oriented, and so on. These databases, along with their database management systems (DBMSs), not only provide a persistence facility, but also manage the information that is persisted. Relational databases are the mostly widely used type. Data in a relational database is modeled as a set of interrelated tables.

The advent of enterprise applications popularized the n-tier architecture, which aims to improve maintainability by separating presentation, business, and database-related code into different tiers (or layers) of the application. The layer that separates the business logic and the database code is the persistence layer, which keeps the application independent of the underlying database technology. With this robust layer in place, the developer no longer needs to take care of data persistence. The persistence layer encapsulates the way in which the data is stored and retrieved from a relational database.

Java applications traditionally used the JDBC (Java Database Connectivity) API to persist data into relational databases. The JDBC API uses SQL statements to perform create, read, update, and delete (CRUD) operations. JDBC code is embedded in Java classes -- in other words, it's tightly coupled to the business logic. This code also relies heavily on SQL, which is not standardized across databases; that makes migrating from one database to another difficult.

Relational database technology emphasizes data and its relationships, whereas the object-oriented paradigm used in Java concentrates not on the data itself, but on the operations performed on that data. Hence, when these two technologies are required to work together, there is a conflict of interests. Also, the object-oriented programming concepts of inheritance, polymorphism, and association are not addressed by relational databases. Another problem resulting from this mismatch arises when user-defined data types defined in a Java application are mapped to relational databases, as the latter do not provide the required type support.

 

When to use iBATIS

iBATIS is best used when you need complete control of the SQL. It is also useful when the SQL queries need to be fine-tuned. iBATIS should not be used when you have full control over both the application and the database design, because in such cases the application could be modified to suit the database, or vice versa. In such situations, you could build a fully object-relational application, and other ORM tools are preferable. As iBATIS is more SQL-centric, it is generally referred to as inverted -- fully ORM tools generate SQL, whereas iBATIS uses SQL directly. iBATIS is also inappropriate for non-relational databases, because such databases do not support transactions and other key features that iBATIS uses.

 

When to use Hibernate

Hibernate is best used to leverage end-to-end OR mapping. It provides a complete ORM solution, but leaves you control over queries. Hibernate is an ideal solution for situations where you have complete control over both the application and the database design. In such cases you may modify the application to suit the database, or vice versa. In these cases you could use Hibernate to build a fully object-relational application. Hibernate is the best option for object-oriented programmers who are less familiar with SQL. 

 

 

When to use JPA

JPA should be used when you need a standard Java-based persistence solution. JPA supports inheritance and polymorphism, both features of object-oriented programming. The downside of JPA is that it requires a provider that implements it. These vendor-specific tools also provide certain other features that are not defined as part of the JPA specification. One such feature is support for caching, which is not clearly defined in JPA but is well supported by Hibernate, one of the most popular frameworks that implements JPA. Also, 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.

 

Comparing persistence technologies

You've now examined three different persistence mechanisms and their operations. Each of these frameworks has its own pros and cons. Let's consider several parameters that will help you decide the best possible option among them for your requirements.

Simplicity

In the development of many applications, time is a major constraint, especially when team members need to be trained to use a particular framework. In such a scenario, iBATIS is the best option. It is the simplest of the three frameworks, because it only requires knowledge of SQL.

Complete ORM solution

Traditional ORM solutions like Hibernate and JPA should be used to leverage complete object-relational mapping. Hibernate and JPA map Java objects directly to database tables, whereas iBATIS maps Java objects to the results of SQL queries. In some applications, the objects in the domain model are designed according to the business logic and might not completely map to the data model. In such a scenario, iBATIS is the right choice.

Dependence on SQL

There has always been a demarcation between the people who are well versed in Java and those who are comfortable with SQL. For a proficient Java programmer who wants to use a persistence framework without much interaction with SQL, Hibernate is the best option, as it generates efficient SQL queries at runtime. However, if you want complete control over database querying using stored procedures, then iBATIS is the recommended solution. JPA also supports SQL through the createNativeQuery() method of the EntityManager.

Support for query languages

iBATIS strongly supports SQL, while Hibernate and JPA use their own query languages (HQL and JPQL, respectively), which are similar to SQL.

Performance

An application must perform well in order to succeed. Hibernate improves performance by providing caching facilities that help with faster retrieval of data from the database. iBATIS uses SQL queries that can be fine-tuned for better performance. The performance of JPA depends on that of the vendor implementation. The choice is particular to each application.

Portability across different relational databases

Sometimes, you will need to change the relational database that your application uses. If you use Hibernate as your persistence solution, then this issue is easily resolved, as it uses a database dialect property in the configuration file. Porting from one database to another is simply a matter of changing the dialect property to the appropriate value. Hibernate uses this property as a guide to generate SQL code that is specific to the given database.

As previously mentioned, iBATIS requires you to write your own SQL code; thus, an iBATIS application's portability is dependent on that SQL. If the queries are written using portable SQL, then iBATIS is also portable across different relational databases. On the other hand, the portability of JPA depends on the vendor implementation that is being used. JPA is portable across different implementations, like Hibernate and TopLink Essentials. So, if no vendor-specific features are used by the application, portability becomes a trivial issue.

Community support and documentation

Hibernate is a clear winner in this aspect. There are many Hibernate-focused forums where members actively respond to queries. iBATIS and JPA are catching up slowly in this regard.

Portability across non-Java platforms

iBATIS supports .Net and Ruby on Rails. Hibernate provides a persistence solution for .Net in the form of NHibernate. JPA, being a Java-specific API, obviously does not support any non-Java platform.

This comparison is summarized in Table 1.

Table 1. Persistence solutions compared

FeaturesiBATISHibernateJPA
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

* The features supported by JPA are dependent on the persistence provider and the end result may vary accordingly.

Conclusion

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.

 

分享到:
评论

相关推荐

    ibatis3中使用jpa的方法进行查询

    本篇文章将探讨如何在Ibatis3中融合JPA的方法进行查询,以实现更加灵活的数据访问。 首先,理解Ibatis3的核心概念。Ibatis3的主要组成部分包括SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession以及Mapper...

    iBatis2学习笔记

    1.iBatis2学习笔记:基本原理和配置.doc 2.iBatis2学习笔记:与Spring2的整合.doc 3.iBatis2学习笔记:单表映射 .doc 4.iBatis2学习笔记:SqlMap的配置总结(18条).doc 5.iBatis2学习笔记:入参和返回值的问题.doc ...

    ibatis和hibernate的简单介绍

    ### ibatis和Hibernate的简单介绍 #### 一、ibatis简介 ibatis是Apache软件基金会的一个开源项目,它提供了一种轻量级的Java持久层框架。ibatis的核心功能是将SQL语句与Java代码分离,使开发人员可以更加专注于...

    iBATIS 三个版对比

    ### iBATIS 三个版本对比分析 iBATIS是一款优秀的持久层框架,它极大地简化了SQL映射编程的复杂度。随着技术的发展,iBATIS经历了多个版本的迭代,包括iBATIS v1、v2以及最新的iBATIS v3。本文将详细对比这三个版本...

    springmvc_hibernate_ibatis_jdbc

    标题中的"springmvc_hibernate_ibatis_jdbc"指的是一个整合了SpringMVC、Hibernate、iBatis和JDBC这四种关键技术的Java应用框架。这个框架旨在提供一个全面且强大的解决方案,便于开发人员进行Web应用程序的构建。 ...

    iBatis和Hibernate的区别

    ### iBatis与Hibernate的主要区别 #### 一、概述 iBatis与Hibernate都是Java领域内流行的ORM(Object-Relational Mapping,对象关系映射)框架,用于简化Java应用程序与数据库之间的交互。尽管它们有着相似的目标,...

    ibatis 与hibernate之间的比较优点和缺点

    ### ibatis 与 hibernate 之间的比较:优点和缺点 #### 概述 在 Java 开发领域中,对象关系映射(Object Relational Mapping,简称 ORM)是一种将面向对象编程语言中的对象模型与数据库系统中的关系模型相互转换的...

    ibatis和hibernate的区别

    【ibatis和Hibernate的区别】 在Java开发中,ibatis和Hibernate都是常见的对象关系映射(ORM)框架,它们简化了数据库操作,将Java对象与数据库表之间的映射关系抽象出来,使得开发人员可以使用面向对象的方式处理...

    ibatis_hibernate3.5_lib.zip

    hibernate下载 : http://sourceforge.net/projects/hibernate/files/hibernate3/--3.XX版本 http://sourceforge.net/projects/hibernate/files/hibernate4/--4.XX版本 IBATS下载地址:...

    IBatisVSHibernate

    标题 "IBatisVSHibernate" 暗示了我们将探讨两个知名的Java持久层框架——IBatis和Hibernate之间的差异和特点。这两个工具都是用于简化数据库操作的框架,但在设计理念和使用方式上有所不同。 **IBatis** 是一个轻...

    Struts2+Spring+Hibernate和Struts2+Spring+Ibatis

    Struts2+Spring+Hibernate和Struts2+Spring+Ibatis是两种常见的Java Web应用程序集成框架,它们分别基于ORM框架Hibernate和轻量级数据访问框架Ibatis。这两种框架结合Spring,旨在提供一个强大的、可扩展的、易于...

    选用ibatis和hibernate的区别

    在选择Java持久化框架时,Ibatis和Hibernate是两个常用的选择。它们各有特点,适用场景也有所不同。以下是对两者的基本功能、使用步骤以及差异的详细分析: **Hibernate** 是一个全面的对象关系映射(ORM)框架,它...

    Struts2 Spring Hibernate IBatis

    Struts2 Spring Hibernate IBatis Struts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatisStruts2 Spring Hibernate IBatis 只需要导入相应的jar包就行了 ,数据库是mysql :数据库名叫做mydatabase,表名...

    ibatis与hibernate的区别

    - **iBatis:** iBatis则更倾向于让开发者自行编写SQL语句,这样可以确保SQL的执行效率,并且便于进行复杂的查询操作。然而,这也意味着开发者需要对SQL有一定的了解才能发挥iBatis的优势。 **2. 对象关系映射的...

    Spring MVC+Hibernate&Ibatis学习 例子 教程

    Spring MVC、Hibernate和iBatis是Java开发中常用的三大框架,它们在Web应用程序开发中各自承担着不同的职责。本教程将深入探讨这三个框架的核心概念、使用方法以及它们之间的协同工作。 **Spring MVC** 是Spring...

    struts2-spring-ibatis-hibernate(hibernate与ibatis集成)

    Struts2、Spring、iBatis 和 Hibernate 是四个在 Java Web 开发中广泛使用的开源框架。这个项目将它们集成为一个整体,为初学者提供了一个学习和理解这些技术如何协同工作的实例。 **Struts2** 是一个基于 Model-...

    Ibatis和Hibernate的分析比较

    在IT领域,数据库持久化是应用开发中的重要环节,Ibatis和Hibernate是两种常见的ORM(对象关系映射)框架,它们都有各自的特点和优势。本文将深入探讨这两者之间的分析比较。 首先,Hibernate是一个全面的对象关系...

    spring与hibernate以及ibatis集成的实例和具体配置图解

    4. 配置Ibatis:编写Mapper接口和XML映射文件,设置SqlSessionFactory的配置。 5. 在Spring中管理DAO:创建DAO接口,实现这些接口并使用@Autowired注解注入SessionFactory或SqlSessionFactory。 6. 测试:编写测试...

    struts1+spring+hibernate+ibatis集成

    5. 使用Spring管理Hibernate和iBatis:通过Spring的HibernateDaoSupport或SqlSessionTemplate来实现对持久层的调用。 6. 测试和调试:确保所有组件协同工作,处理可能出现的依赖冲突和配置问题。 这种集成方式的...

Global site tag (gtag.js) - Google Analytics