Data Persistence layer Framework Ibatis, Hibernate vs. JPA comparison

Source: Internet
Author: User
Tags ruby on rails

In this article we describe and compare two of the most popular open source persistence frameworks: Ibatis and Hibernate, and we'll also discuss the Java Persistence API (JPA). We introduce each solution and discuss the quality that it provides, as well as its strengths and weaknesses in a wide range of application scenarios. Then we compare Ibatis, Hibernate, and JPA based on factors such as performance, portability, complexity, and adaptability to changes in the data model.

If you are a beginner Java programmer, new to the concept of persistence, read this article as an initiation to accept this topic and most popular open source persistence solutions. If you are familiar with these three solutions and simply want a simple comparison, you will find the appropriate content in the "Compare Persistence Technology" section.

Understanding Persistence

Persistence ( Persistence ) is a property of the data that ensures that data is available even outside the life cycle of the app. For object-oriented languages like Java, persistence ensures that the state of an object remains accessible even after the application that created the object has stopped executing.

There are several ways to implement persistence. The traditional way to solve this problem is to use a file system to store the required information in a flat file (flat files). This approach is difficult to manage large amounts of data because the data is distributed across different files. It is also a problem to maintain data consistency using a flat file system, because the same information may be duplicated in individual files. Finding data in a flat file is time consuming, especially if these files are not yet sorted. Also, file systems have limited support for concurrent access because they do not guarantee the integrity of the data. For all these reasons, the file system is not considered a good data storage solution in the quest for persistence.

The most common approach now is to use a database that acts as a repository for huge amounts of data. There are many types of databases: relational, hierarchical, network, object-oriented, and so on. These databases, as well as their database management systems (DBMS), provide not only the persistence capability, but also the management of their persistent information. Relational databases are the most widely used types, and data in relational databases is modeled as a set of interrelated tables.

The advent of enterprise-class applications has introduced an n-tier architecture designed to improve maintainability by separating performance, business, and database-related code into different tiers (or layers ) of the application. The level of separation of business logic and database code is the persistence layer , which maintains the independence of the application relative to the underlying database technology. With this robust level in place, developers no longer have to worry about the persistence of data. The persistence layer encapsulates how the data in the relational database is stored and retrieved.

Java applications traditionally use the JDBC (Java Database Connectivity) API to persist data to relational databases. The JDBC API uses SQL statements to complete creation (create), read, update, and delete (CRUD) operations. The JDBC code is embedded in the Java class-in other words, this type of code is tightly coupled to the business logic. This type of code also relies heavily on SQL, which is not a cross-database standard, which makes porting from one database to another difficult.

Relational database technology emphasizes data and its relationships, while the object-oriented paradigm used in Java does not focus on the data itself, but on the operations performed on the data. Therefore, when these two technologies need to work together, there is a conflict of interest. Furthermore, relational databases do not satisfy the concepts of inheritance, polymorphism, and association of these object-oriented programming. When a user-defined data type in a Java application is mapped to a relational database, another problem that results from this mismatch arises because the latter does not provide the type support that is required.

Object Relational Mapping

Object Relational Mapping (Object-relational Mapping,orm) has become a solution to this problem, sometimes referred to as Object-relational impedance mismatch (impedance mismatch). ORM is a technique for transparently persisting an Application object to a table in a relational database. The ORM behaves like a virtual database, hiding the underlying database schema from the user. ORM provides functionality to perform full CRUD operations and encourages object-oriented queries. ORM also supports metadata mapping and helps with application transaction management.

An example helps illustrate how ORM works. Consider a simple car object that needs to be persisted into the database, which is the car object in the domain model as a representation of the car table in the data model. The properties of the car object are derived from the columns of the car table. There is a direct mapping between the car class and the car table.

There are many open-source ORM tools, including Hibernate, IBATIS SQL maps, and Java ultra-lite persistence. Most of these tools are a persistence framework that provides an abstraction layer between Java applications and databases. The persistence framework maps objects in the application domain into data that needs to be persisted in the database, which can be defined using XML files or metadata annotations (the latter being introduced into the language as part of the Java1.5). The purpose of the persistence framework is to decouple database-related code and application code (that is, business logic) to improve application flexibility. The persistence framework simplifies the development process by providing a wrapper for the persistence logic.

Having completed the basic introduction to persistence, we are ready to go on to discuss two of the most popular open source persistent frameworks Ibatis and hibernate. We'll also introduce the Java Persistence API and discuss the strengths and weaknesses of these three solutions in a variety of scenarios.

When to use Ibatis

Ibatis is best used when you need to have full control over SQL, which is useful when you need to fine-tune SQL queries. When you have complete control over both the application and the database design, you should not use Ibatis, because in this case the application may make changes to the database, or vice versa. In this case, you can build a complete object-relational application, and other ORM tools are more suitable, because Ibatis is more SQL-centric, often called inverted -full-featured ORM tools that generate SQL, and ibatis directly uses SQL. Ibatis is also not suitable for non-relational databases, because such databases do not support transactional and other Ibatis key features.

When to use Hibernate

Hibernate is best used as a means of end-to-end or mapping. It provides a complete ORM solution, but does not let you control the query. Hibernate is an ideal solution for situations where there is complete control over both the application and the database. In such cases, you can modify the application to apply to the database, and vice versa, in which case you can use Hibernate to build a full-object-relational application. Hibernate is the best choice for object-oriented programmers who are less familiar with SQL.

When to use JPA

JPA should be used when a standard Java-based persistence solution is required. JPA supports the two object-oriented programming features of inheritance and polymorphism. The disadvantage of JPA is that it requires a provider that implements its own. These vendor-specific tools also provide some features that are not defined as part of the JPA specification, one of which is cache support, which is not explicitly defined in JPA, but one of the most popular implementations of JPA's framework hibernate provides a good support for this feature.

In addition, JPA is defined to work only on relational databases. If your persistence solution needs to be extended to other types of data storage, such as on an XML database, then JPA will not be able to solve your persistence problem.

Comparison of persistence Technologies

Now you have analyzed three different persistence mechanisms and how they work. Each of these frameworks has its own strengths and weaknesses. Let's consider a few parameters that can help you determine the best possible solution to your needs.

Ease of

Time is a major constraint in the development of many applications, especially when team members need to be trained to use a particular framework. In such cases, Ibatis is the best choice, and the framework is the simplest of the three frameworks, because it requires only the knowledge of SQL.

Full- ORM Solution Solutions

Traditional ORM solutions like hibernate and JPA should be used as a complete object-relational mapping tool. Hibernate and JPA map Java objects directly to database tables, while Ibatis maps Java objects to the results of SQL queries. In some applications, objects in the domain model are designed according to business logic and may not be fully matched to the data model, in which case the ibatis is the appropriate choice.

the SQL the dependency

There will always be a division of Java-savvy people and more trusted SQL, and for a skilled Java programmer to use a persistence framework that does not require much interaction with SQL, Hibernate is the best choice because it generates efficient SQL queries at run time. However, if you want to use stored procedures to control all aspects of database queries, then Ibatis is the recommended solution. JPA can also support SQL through the Entitymanager createnativequery () method.

Supported 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

A good performance is required for an application to be successful. Hibernate improves performance by providing caching facilities that help to retrieve data from the database more quickly. Ibatis uses SQL queries, which can be fine-tuned for better performance. The performance of JPA depends on the vendor's implementation, making choices based on the specific circumstances of each application.

Portability across different databases

Sometimes you need to change the relational database used by your app, and if you use Hibernate as a persistent solution, this is an easy problem to solve because Hibernate uses a database dialect attribute in the configuration file. Porting from one database to another is only a matter of modifying the Dialect property to an appropriate value. Hibernate uses this property as a guide for generating SQL code that is specific to a given database.

As mentioned earlier, Ibatis requires you to write your own SQL code, so the portability of the Ibatis app depends on these SQL. If the query is written using portable SQL, then Ibatis can also be ported between different relational databases. On the other hand, the portability of JPA depends on the vendor implementation that it is using. JPA can be ported between different implementations, such as Hibernate and TopLink essentials. Therefore, portability is not a big problem if the application does not use some provider-specific feature features.

Community support and documentation

In this regard, Hibernate is clearly a winner. There are many hibernate-focused forums in which community members respond positively to various questions. On this point, Ibatis and JPA are catching up slowly.

across non- Java portability of the platform

Ibatis supports. NET and Ruby on Rails. Hibernate provides a durable solution for. NET in the form of nhibernate. JPA, as a Java-specific API, clearly does not support any non-Java platform.

A summary of this comparison is given in table 1.

Table 1. Comparison of durable solutions

Characteristics

IBATIS

Hibernate

Jpa

Ease of

Excellent

Liang

Liang

A complete ORM Solution

So so

Excellent

Excellent

Adaptability to changes in the data model

Liang

So so

So so

Complexity

Excellent

So so

So so

The dependency on SQL

Liang

So so

So so

Performance

Excellent

Excellent

Not applicable *

Portability across different relational databases

So so

Excellent

Not applicable *

Portability of non-Java platforms

Excellent

Liang

Not supported

Community support and documentation

So so

Liang

Liang

*JPA support for these features depends on the persistence provider, and the end result may vary depending on the situation.

Conclusion

IBATIS, Hibernate, and JPA are the three different mechanisms used to persist data to relational databases, each with its own strengths and limitations. Ibatis does not provide a complete ORM solution, nor does it provide any direct mapping of object and relational models. However, Ibatis provides you with full control over your queries. Hibernate provides a complete ORM solution, but does not provide control over the query. Hibernate is very popular and has a large and vibrant community to support new users. JPA also provides a complete ORM solution and provides support for object-oriented programming features such as inheritance and polymorphism, but its performance depends on the persistence provider.

The selection of a particular persistence mechanism is a trade-off of all features, and these features are discussed in the comparison section of this article. For most developers, decisions need to be made based on whether they require full control of the application's SQL, whether they need to generate SQL automatically, or simply want a complete ORM solution that is easy to program.

Data Persistence layer Framework Ibatis, Hibernate vs. JPA comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.