ISSN: 2229-371X
B.Vasavi*1, Y.V.Sreevani2, G.Sindhu Priya3
|
Corresponding Author:B.Vasavi, E-mail: vasavi.bande@yahoo.co.in |
Related article at Pubmed, Scholar Google |
Visit for more related articles at Journal of Global Research in Computer Sciences
This paper discusses hibernate technology as a novel and efficient means to access huge databases and also focuses on how to implement persistent features in object-oriented system through it . It discusses currently available hibernate mapping framework in detail. Hibernate provides support for collections, object relations, as well as complex and composite types. In addition to persisting objects, hibernate also provides a rich query language to retrieve objects from the database, along with an efficient caching layer and Java Management Extensions (JMX) support. Hibernate is a powerful, high-performance, feature-rich and very popular ORM solution for Java. Hibernate facilitates development of persistent objects based on the common Java object model to mirror the underlying database structure. This approach progresses the business performance to some extent, advances development efficiency exceedingly and obtains preferable economical efficiency and practicability. In addition to, it compares and analyzes the database access efficiency resulted from two mechanisms based on Hibernate and JDBC. This paper offers insight into hibernate technology its implementation and usage.
Keywords |
Hibernate, HQL, ORM,, Database, SQL. |
INTRODUCTION |
A major portion of the development of an enterprise application involves the creation and maintenance of the persistence layer used to accumulate and retrieve objects from the database of choice [1]. Many organizations resort to create homegrown, often buggy, persistence layers. If changes are made to the underlying database schema, it can be expensive to disseminate those changes to the rest of the application. Hibernate steps in to fill this gap, providing an easy-to-use and powerful object relational persistence framework for Java applications. Hibernate is an objectrelational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves Object-Relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions. ORM is a piece of software product for the representation and translation of data between the database and the object-oriented programming language. Hibernate is one such ORM solution and it is an open-source project. The Hibernate 2.1 framework has won a award in 2005. Hibernate provides support for collections and object relations, as well as composite types. It also provides a rich query language to retrieve objects from the database, a competent caching layer and has Java Management Extensions (JMX) support. Hibernate is released under the lesser GNU Public License, which is sufficient for use in commercial as well as open source applications. It supports numerous databases, including Oracle and DB2, also popular open source databases such as PostgreSQL and MySQL. |
Working of Hibernate |
Rather than utilizing bytecode processing or code generation, hibernate uses runtime reflection to determine the persistent properties of a class. The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object. The mapping documents are compiled at application startup time and supply the framework with necessary information for a class [2]. In addition to it, they are used in support operations, such as generating the database schema or creating stub and Java source files. A Session Factory is created from the compiled collection of mapping documents[6]. The Session Factory provides the mechanism for managing persistent classes and the Session interface. The Session class provides the interface between the persistent data store and the application. The Session interface wraps a JDBC connection, which can be usermanaged or controlled by hibernate, and is only intended to be used by a single application thread, then closed and discarded. |
Hibernate Architecture |
The following Figure1and Figure 2 describes the high level architecture of hibernate i.e. they show how hibernate uses the database and configuration data to provide persistence services (and persistent objects) to an application. To use Hibernate, it requires creating Java classes that represent the table in the database and then map the instance variable in the class with the columns in the database. Then, Hibernate can be used to perform operations on the database like select, insert, update and delete the records in the table. Hibernate automatically creates the query to perform these operations. The Figure1describes the high level architecture of hibernate. |
Hibernate architecture has following three main components. |
Connection management: |
Hibernate Connection management service grant efficient management of the database connections. Database connection is the priciest part of database as it requires a lot of resources of open/close the database connection. |
Transaction management: |
Transaction management service provides the capability to the user to execute more than one database statements at a time. |
Object relational mapping: |
Object relational mapping is a technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update, view and delete the records form the underlying table. When we pass an object to a Session.save() method, hibernate reads the state of the variables of that object and executes the necessary query. Hibernate is extremely good tool as far as object relational mapping is concern, but in terms of connection management and transaction management, it lacks in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the hibernate. Hibernate provides a lot of flexibility in usage. It is called "Lite" architecture when we only use object relational mapping component. While in "Full Cream" architecture all the three component Object Relational mapping, Connection Management and Transaction Management are used. Hibernate architecture can be shown in detail in the figure 2. |
CREATING THE SESSION FACTORY |
The SessionFactory stores the compiled mapping documents specified when the factory is created. Configuring the SessionFactory is fairly straightforward as shown in figure 4. All of the mappings are added to an instance of net.sf.hibernate.cfg.Configuration, which is then used to create the SessionFactory instance. |
Configuration cfg = new Configuration() |
addClass(example.Player.class) |
addClass(example.Team.class); |
SessionFactory factory = cfg.buildSessionFactory(); |
The Configuration class is only needed for the creation of the SessionFactory and can be discarded after the factory is built. Instances of Session are obtained by calling SessionFactory.openSession(). The logical lifecycle of a Session instance is the span of a database transaction. The SessionFactory can also be configured using an XML mapping file, placed in the root of your classpath [8]. The evident advantage to this approach is that your configuration isn’t hardcoded in the application. |
INTERCEPTORS IN HIBERNATE |
Hibernate provides an ORM solution for persisting and querying data in the database. A Hibernate application can be structured in a way such that certain methods can be made and to be invoked when a particular life-cycle event occurs. Not always the API in a software/product will completely satisfy the application needs and requirements. Hibernate is no more away from this [9]. Therefore, Hibernate API is designed in such a way to provide pluggable framework through the concept of Interceptors. In a multi-tiered application, the situation for the inclusion of Interceptors can happen at any level. It can happen at the Client level, Server level and even at the persistence level. Imagine an application is saving employee records in a database and now the application mandates to display to the Database admin about the history of inserts and updates. |
can use the Session.find() methods. The find () method allows you to pass an HQL (Hibernate Query Language) statement and retrieve matching objects as a java.util.List. The find () method has three signatures, allowing you to pass arguments to JDBC-like “?” parameters as a single argument, named parameters, or as an Object[]. |
Deleting Persistent Classes |
Making a persistent object transient is accomplished with the Session.delete () method. This method supports passing either a specific object to delete or a query string to delete multiple objects from the database. |
// method 1 – deleting the Player. |
session.delete(player); |
// Example 2 – deleting all of the Players with a salary greater than 4 million |
session.delete(“from player in class example.Player where player.annualSalary > 4000000”); |
It’s important to note that while the object may be deleted from the database, your application may still hold a reference to the object. Deleting an object with collections of objects, such as the Team’s set of Players, can cascade to child objects by specifying cascade=”delete” for the set element in the mapping document. |
Collections |
Hibernate can manage the persistence of object collections [5], whether they are Sets, Maps, Lists, arrays of objects or primitive values. It also allows a different form of collection called a “bag”. A bag can be mapped to a Collection or List, and contains an unordered, unindexed collection of entities [6]. Bags can contain the same element many times. Additional semantics supported by implementing classes, such as Linked List, are not maintained when persisted. Another note is that the property of a collection must be the interface type (List, Map, Set). This is because, in order to support lazy collections, hibernate uses its own implementations of the List, Map or Set interfaces. When accessing a lazily initialized collection, it’s important to remember that a Session must be open, or an exception will be thrown as given below. |
Performance Considerations |
Fortunately this functionality doesn’t come at much of a performance cost. The hibernate website claims that its “overhead is much less than 10% of the JDBC calls,” and our experience in deploying applications using hibernate supports this. Hibernate can make multiple optimizations when interacting with the database, including caching objects, efficient outer join fetching and executing SQL statements only when needed [9]. It is difficult to achieve this level of sophistication with hand-coded JDBC. |
ADVANTAGES AND DISADVANTAGES OF HIBERNATE |
Hibernate is better than plain JDBC: You can use hibernate which generates the SQL very easily and then automatically executes the necessary SQL statements [10]. This saves a lot of development and debugging time of the developer. Writing JDBC statement, setting the parameters, executing query and processing the result by hand is plenty of work. Hibernate will save all tiresome efforts. |
Mapping of Domain object to relational database: Hibernate maps your domain object with the relational database [3]. Now you can concentrate on your business logic rather than managing the data in the database. |
Light weight database-independent ORM solution |
Layered architecture: Hibernate is layers architecture and you can use the components as per your application need. |
CONCLUSION |
This paper has illustrated an introduction to what hibernate can do. The analyses how hibernate delivers a high performance, open source persistence framework comparable to many of its open source and commercial counterparts. Developers utilizing Hibernate can greatly reduce the amount of time and effort needed to code, test, and deploy applications. Hibernate is a powerful, highperformance, feature-rich and very popular ORM solution for Java along with mapping objects to a database. As discussed above hibernate also provides advanced data query and retrieval services through HQL, efficient caching, and other optimization techniques with useful built-in utilities for coding and schema generation. This automats the generation of a persistent layer to a large extent and hence, helps in relieving the developer up to 95% of common persistence related coding. |
References |
|