This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.
Object-Relational Mapping (ORM) frameworks allow software developers to control objects that persist in relational tables. In the environment of the Java language, there are a number of ORM frameworks exist. These frameworks give balancing of factors between richness of functionality, usability and performance. This paper compares two such frameworks, namely Hibernate and iBatis in terms of their functionality, usability and performance. Usability and functionality are assessed by means of an action research study in which two sample applications from different domains were created separately using the two frameworks, and the two implementation efforts were compared in terms of several factors, including programming effort and number of lines of code. This research work is taken as reference to compare the functionality, usability and performance for this paper.
The majority of enterprise applications use relational databases for data persistence, but they are frequently used object-oriented programming language in which data is structured as objects. This difference in representation concept leads to a phenomenon known as impedance mismatch. Impedance mismatch refers to the differences between Object-oriented technology and relational technology. Object oriented technology depends on objects that have data and behavior. The objects have identity and are pass over using direct access whereas relational databases systems (RDBS) store data into tables. That data is controlled and accessed using Data Manipulation Language (DML) such as SQL. Furthermore in RDBS tables have primary key and data is related with foreign keys and they do not have same inheritance logic as object's have.
A representative example of impedance mismatch is when storing a string into varchar type into database. String in many programming languages can be as long as wanted, but data type varchar in database can be only 256 characters. In application everything may work when using longer values, but when storing that value into database an error will occur.
One way to solve the impedance mismatch is to introduce an abstract layer, called a persistence layer. The persistence layer lies between RDBS and object model of the application. This way the database is fully encapsulated from application and most requests to database are done through a persistence layer. That layer validates all data that will be inserted into database.
A popular approach used by persistence framework nowadays is called object-relational mapping (ORM). An ORM framework transforms and transparently moves data from the form it is managed in main memory (objects) to a persistent relational data store and vice-versa. In simpler terms, the ORM tool helps to manage the database access and the mapping between the database and objects. There are three patterns that can be used when creating an ORM tool - Table data gateway, Row data gateway and Active record.
Table Data Gateway
Table Data Gateway is a design pattern in which an object acts as a gateway to a database table . "Table data gateway holds all the SQL for accessing a single table or view: selects, inserts, updates and deletes. Other code calls its methods for all interaction with the database." as explained by Martin Fowler . The Table data gateway pattern has a very simple interface which has several methods for create, read, update, delete (CRUD) actions. Each of these methods map input into an SQL call and executes it. Usually one class is made for each table `.
Row Data Gateway
Row data gateway is an object that acts as a single row in a database. "It gives us objects that look exactly like the record in our record structure but can be accessed with the regular mechanisms of our programming language. All details of data source access are hidden behind this interface." as mentioned by Fowler .
"Active record is an approach to access data in a database. A database table or view is twined into a class, thus an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database; when an object is updated, the corresponding row in the table is also updated. The wrapper class implements "accessor" methods or properties for each column in the table or view." . Active record is very similar to Row data gateway as they both contain database access. The main difference with respect to row data gateway is that active record also contains domain logic.
Java has persistence tools from 1999. It has many open source persistence frameworks like hibernate, iBatis, Apache OpenJPA, Toplink, SimpleORM. The list goes on, there are many orm frameworks for java. There are about 36 open source orm frameworks for java. Hibernate was developed by a team headed by Gavin King. The development of Hibernate began in 2001 and the team was later acquired by JBoss, which now manages it. Hibernate was developed initially for Java; in 2005 a .Net version named NHibernate was introduced. The iBATIS project was initiated by Clinton Begin and released in 2001. At the first iBATIS was designed to use it for java but then it extended to ohrer platforms like .Net and Ruby.
Hibernate is a very popular open source ORM library for the Java language. Hibernates main feature is mapping from Java classes and data types to database tables and types. It provides data query (Hibernate language query, HQL) and retrieval facilities. Developer is relieved from manual data to object conversion and it keeps application portable to all supported SQL databases. A sample of hibernate XML mapping language is given below.
In this example, we have Employee described in XML. Employee has properties named id, employee first name and employee last name. A database table is identified by mapping the mapping file and configuration file to a specific persistent class in the database. The mapping file maps related columns to the related fields. The mapping also contains the associations, collections and primary key mapping if any and ID key generation mechanisms. Generally the names for mapping files are based on the tables to which they have to map; in the above application example, we can use Employee.hbm.xml for the file that relates to the EMPLOYEE table.
The configuration file is used to launch a a connection to a specific relational database and this file should also know to which it has to refer. The configuration file should know which mapping file it needs to refer to. Hiberante will read the file then it constructs a dynamic java class related to that table in the database.
To commit the queries Hibernate uses some factories like session to create an object of type session and an other object called transaction used for transactions. When an object of type SessionFactory is generated in the program, the framework will read the configuration file and points to the corresponding mapping file. The session instance that is generated at the SessionFactory gets a specific connection to the database. The session instance persistence ambiance for the object of a persistence class. The insatnces have to be in one of the following states: 1) transient, 2) persistent, 3)detached. In the first state, the instance is not correlated to the table; in the second state, the instance is correlated to the table; and in the third state, there is no assurance that the instance is in association with the table.
In Hibernate, data loading is done by means of a session object. Session class has method named "load" which takes ID as an argument and returns Author object from database.
Deleting also relies on a session object. The Session class has a method named "delete"
which takes object as argument. This object is deleted from database.
iBATIS is a persistence mechanism that prevents the intricacy of JDBC but gears the advantages of the SQL. It is not like many other persistence frameworks, iBATIS ensures that all the advantages of SQL are not overridden but encourages the direct use of SQL by the persistence framework.
It gives the developer a very simple mapping and API layer, the simplicity of iBATIS has many advantages that can be used to construct data-access code. In this iBATIS framework we need not map exclusively objetc model and the data model precisely. This framework iBATIS uses a XML descriptor to map the objects or stored procedures, statements and result sets to a data mapper. This framework doesn't use any metadata mapper to map objects to the data in the tables to the database. The framework has the ability to enable the data model and the object model to be autonomous of each other. A sample of iBATIS XML mapping language is given below.
To configure a data source the SQLMapconfig.xml uses a transaction manager to map to a particular SQL map. Along with some details like data about the driver, the database URL and the username and password it also provides the types of the data source that are used by the application.. To load the sqlMap.xml the sqlMap is used to specify the location.
After some practice a file is named as SQLMap.xml, which relates to the table in the database. In a single application there can be a more number of such files to relate to a table in the database. The domain objects are mapped to SQL statements in this file. A SQL ResultSets is used to map the input statements to the result, a parameter map is used as the descriptor in this file and this file also contains the queries. So we need to change the XML code not the java code, this makes the life simple instead of digging a very large number of lines of code. The actual SQL statements interact with the database to do the mapping.
iBATIS is easy to understand and the developer has the greater flexibility working with this framework if the person knew the SQL programming. To perform the CURD operations like select, insert, update and delete there is an SQLMap.xml file which defines the SQL statements. To avoid the typing full name every time iBATIS provides a tag called typeAlias to represent the type aliases for the class names. To tell the mapping between the columns returned from a query to the properties of the persistence class namely Employee class in our example it uses a resultMap tag. If the properties in the class and the columns in the table match exactly the result map tag is not necessary, this is optional. A series of queries are followed by this resultMap, this xml may contain any number of queries like select, insert , update and insert statements.with in their respective tags. Each and every SQL statement is provided by a unique id number to identify the SQL statements. We can map the result of a query to a resultMap tag or to the JavaBean class. The attributes in the class and the aliases in the query should be same. To specify the bean class whose properties are the inputs the parameterClass property is used. The properties of the class (JavaBean) are denoted by hash symbol .
The java application need to load the SQLMapConfig.xml file when it completes the entire configuration and mapping in the both the files. SQLMap.xml is the first configuration file to be loaded, this file is created earlier. There is a resource class in the iBATIS framework called com.ibatis.common.resources.Resources, we have to use this file.
We need to provide a client class to work with SQLMaps, it is called SqlMapClient. It allows us to run mapped statements like select, insert, update, and so on. One object is enough because it is thread safe. Thus as a static member it is a very good one. SQLMapConfig.xml file creates an object by reading a line. Using the SQLMapConfig.xml can be read by an utility present in the framework , and the utikity is Resources.getResourceAsReader(). In this example we have to access the data for Employee, so by using the SQLMap we can create an instance, this instance is helpful in getting the object for the Employee class and access the data. The framework provides many methods to access the data from the database like queryForList(), insert(), queryForMap(), these are the methods used to invoke operations. There are other methods available for the user to access the data, the above method is used to returns a more than one result, if the user wants only one result there is an other method queryForObject(). This type of methods are also present in JPA. We can use different methods to access the rows from the tables. The two methods takes some parameters, these parameters are none other than statement name. In addition to the above mentioned methods there are other methods like insert, delete and update which makes the developer life simpler in developing the application using iBATIS. The class object and the statement name is taken as the parameter. The map file contains the statement name.
Different classes of application require different sets of features from an ORM framework. In some applications, the data structures controlling are fairly simple (e.g. simple one-way relationships between classes) . Other applications manipulate more complex data structures including bi-directional relations. Similarly, in some applications, full support for transactions is important, while smaller applications may or may not require it. Below, we list a number of features that one may expect to find in an ORM framework. The list includes features related to the data structures, support for integrity (transactions) and optimization (e.g. memory caching), and additional features such as query language, supported databases, annotations, complexity, performance, dependence on sql, flexibility.
Custom Query Language
Yes (HQL) / (EJB QL)
One way relationship
Community support documentation
Complete ORM solution
Portability across different relational databases
Portability to non- java platforms
Dependence on Sql
For an end-to-end object relational mapping Hibernate is the best choice. It gears a fully ORM quick fix, but hands down us on control over queries. If we have control over on both the application and the database design we can use this framework to persist the data. In these type of cases we need to modify the application to meet the database, or vice versa. These type of cases could use Hibernate to construct a completely object-relational application. This framework is the good choice for OO programmers and who have less familiarity with SQL. It means Programmers need not be geeks in sql to use Hibernate. This is very good option for the programmers and the company. In the way that the programmers need not learn sql language and the company need not hire other people to work for database designing and writing sql statements.
iBATIS is best suited for the application where we can have complete control over the SQL. The queries can be fine grained using this framework as we have full control over the SQL. This framework is not suitable where the developing application has control on both the application and database design. If we have control on both then the application can be modified in according to the database and vice versa. In this type of situations there are other ORM tools available which can be build using a object-realtional. This framework is mostly concentrated on SQL, because many ORM frameworks generates SQL but this framework uses SQLanguage directly. This framework is not suitable for other databases like non-relational because in this databases there is no support for transactions and other important features that this framework has. This is a very good option for the company who have already database designers ans sql statement writers in the company.
Persistence frameworks are tools that can move data from their most natural form to a more permanent data store. They are meant to address the impedance mismatch between object-oriented programming languages and relational database systems. The most common patterns used by these frameworks are table data gateway, row data gateway and active record.
Hibernate, iBATIS are two different implementations to persist data in a relational database. The two frameworks has its own boundaries. iBATIS does not gear us with fully object relation mapping solution, and do not give opportunity to map objects to relational models. iBATIS gears us with complete control over queries. Where as Hibernate gears a fully object relational mapping solution, but user has no flexibility in writing the queries. Hibernate is a very familiar and has a very large active community which provides support for new users.
The selection of a specific persistence implementation depends on all the features discussed in the above section. What an application should use depends on the requirement of their project. If they want complete control over the SQL they can use the iBATIS other wise they can use the Hibernate, which gives the complete ORM solution..