Simplify Java Object Persistence with Hibernate

Simplify Java Object Persistence with Hibernate

toring and retrieving information for most applications usually involves some form of interaction with a relational database. This has presented a fundamental problem for developers for quite some time since the design of relational data and object-oriented instances share very different relationship structures within their respective environments. Relational databases are structured in a tabular configuration and object-oriented instances are typically structured in a hierarchical manner. This “impedance mismatch” has led to the development of several different object-persistence technologies attempting to bridge the gap between the relational world and the object-oriented world. The Hibernate persistence framework provides yet another means for bridging this gap.

This article is the second in a series discussing how three different object-persistence technologies (EJB, Java Data Objects, and Hibernate) attempt to simplify the chore of connecting relational databases and the Java programming language.

Introducing Object Persistence
The task of persisting Java objects to a relational database is currently being facilitated by a number of different tools which allow developers to direct persistence engines in converting Java objects to database columns/records and back. This task involves serializing hierarchically-structured Java objects to a tabular-structured database and vice versa. Essential to this effort is the need to map Java objects to database columns and records in a manner optimized for speed and efficiency.

The Hibernate framework tackles the Java-object-to-database problem as elegantly as any framework currently available. Hibernate works by persisting and restoring plain old Java Objects (POJOs) using a very transparent and low-profile programming model.

An Overview of Hibernate
Hibernate is a Java framework that provides object/relational mapping mechanisms to define how Java objects are stored, updated, deleted, and retrieved. In addition, Hibernate offers query and retrieval services that can optimize development efforts within SQL and JDBC environments. Ultimately, Hibernate reduces the effort needed to convert between relational database result-sets and graphs of Java objects.

One of its unique features is that Hibernate does not require developers to implement proprietary interfaces or extend proprietary base classes in order for classes to be made persistent. Instead, Hibernate relies on Java reflection and runtime augmentation of classes using a powerful, high-performance, code-generation library for Java called CGLIB. CGLIB is used to extend Java classes and implement Java interfaces at runtime.

The Hibernate Configuration File
You can configure the Hibernate environment in a couple of ways. One standard way that proves very flexible and convenient is to store the configuration in a file named hibernate.cfg.xml. You place the configuration file at the root of a Web application’s context classpath (e.g. WEB-INF/classes). You then access the file and read it using the net.sf.hibernate.cfg.Configuration class at runtime.

The hibernate.cfg.xml file defines information about the database connection, the transaction factory class, resource mappings, etc. The following code demonstrates a typical configuration file:

            org.hsqldb.jdbcDriver      jdbc:hsqldb:data/userejb      sa            true      net.sf.hibernate.dialect.HSQLDialect               net.sf.hibernate.transaction.JDBCTransactionFactory                     

The Hibernate Mapping Configuration File
Hibernate applications make use of mapping files containing metadata defining object/relational mappings for Java classes. A mapping file is designated with a suffix of .hbm.xml. Within each configuration file, classes to be made persistent are mapped to database tables and properties are defined which map class-fields to columns and primary keys. The following code illustrates a typical Hibernate configuration file named UserInfo.hbm.xml:

                                                                                                            

Hibernate Sessions
In order to make use of Hibernate’s persistence mechanisms, you must initialize the Hibernate environment and obtain a Session object from Hibernate’s SessionFactory class. The following snippet of code illustrates these processes:

// Initialize the Hibernate environmentConfiguration cfg = new Configuration().configure();// Create the session factorySessionFactory factory = cfg.buildSessionFactory();// Obtain the new session objectSession session = factory.openSession();

The call to Configuration().configure() loads the hibernate.cfg.xml configuration file and initializes the Hibernate environment. Once the configuration is initialized, you can make any additional modifications you desire programmatically. However, you must make these modifications prior to creating the SessionFactory instance.

An instance of SessionFactory is typically created once and used to create all sessions related to a given context.

A Hibernate Session object represents a single unit-of-work for a given data store and is opened by a SessionFactory instance. You must close Sessions when all work for a transaction is completed. The following illustrates a typical Hibernate session:

Session session = null;UserInfo user = null;Transaction tx = null;try{   session = factory.openSession();   tx = session.beginTransaction();   user = (UserInfo)session.load(UserInfo.class, id);   tx.commit();}catch(Exception e){   if (tx != null)   {      try      {         tx.rollback();      }      catch (HibernateException e1)      {         throw new DAOException(e1.toString());      }   }   throw new DAOException(e.toString());}finally{   if (session != null)   {      try      {         session.close();      }      catch (HibernateException e)      {      }   }}

The Hibernate Query Language
Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate Query Language (HQL), is an object-oriented extension to SQL. HQL allows access to data in a variety of ways including object-oriented queries, as in the find() method of the Session object illustrated by the following example:

List users =   session.find("from UserInfo as u where u.fullName = ?",                "John Doe",                Hibernate.STRING);

You can construct dynamic queries using Hibernate’s criteria query API:

Criteria criteria = session.createCriteria(UserInfo.class);criteria.add(Expression.eq("fullName", "John Doe"));criteria.setMaxResults(20);List users = criteria.list();

If you prefer to use native SQL, you may express a query in SQL, using createSQLQuery():

List users =   session.createSQLQuery("SELECT {user.*} FROM USERS AS {user}",                           "user",                          UserInfo.class).list();

Large numbers of objects returned from a query will be loaded as needed when one of the iterate() methods is used. The iterate() methods typically offer better performance since they load objects on demand:

Iterator iter =   session.iterate("from UserInfo as u where u.city = New York"); while (iter.hasNext()){   UserInfo user = (UserInfo)iter.next();   // process the user object here}

The Application and Runtime Environment
This article will use JBoss 3.2.3 as the deployment and runtime environment for the examples that follow. You’ll design a simple Web application that allows user accounts to be created and retrieved using a Web browser. Client requests will be passed from a browser to a Java servlet, which communicates with a user service, which communicates with Hibernate-based data access objects (DAOs), as shown in Figure 1.

Figure 1. Client Requests: This image shows the steps through which a a client request is processed.

The DAO pattern abstracts and encapsulates all access to the data source. The application has one DAO interface, UserDao. The implementation class, HibernateUserDao contains Hibernate-specific logic to handle data-management duties for a given user.

You must construct or modify some of the configuration files to accommodate the needs of Hibernate. First, modify the jaws.xml file to define the datasource for the application:

jaws.xml   java:/DefaultDS   Hypersonic SQL

Next, modify the hibernate.cfg.xml file to define the Hibernate properties that will be loaded by the application when Hibernate is configured. Among other things, the environment is configured to use HSQL as the database and a mapping resource is defined for the UserInfo class:

hibernate.cfg.xml            org.hsqldb.jdbcDriver      jdbc:hsqldb:data/userejb      sa            true      net.sf.hibernate.dialect.HSQLDialect               net.sf.hibernate.transaction.JDBCTransactionFactory                     net.sf.hibernate.cache.HashtableCacheProvider            update               

The Web Tier Configuration
Each client HTTP request is handled by a FrontController-style servlet embodied within an instance of UserInfoServlet. The UserInfoServlet instance converts each request to a business-service request and then calls the appropriate business service for processing.

The UserInfoServlet is shown in Listing 1.

The Business Tier
Each client HTTP request is converted to a business-service request and passed to the appropriate business service for processing. Each business service object performs the necessary business logic and makes use of the appropriate DAO for data-store access.

The UserService class encapsulates methods for operating on UserInfo objects including storing, updating, deleting, and retrieving instances of UserInfo. The UserService class is shown in Listing 2.

The UserService class makes use of the UserInfo class, which represents a given user. The UserInfo class is shown in Listing 3.

The UserInfo class represents a given user and is configured for Hibernate in the file, UserInfo.hbm.xml.

                                                                     

The Data Tier
Each business-service request is passed to the appropriate business service for processing. A business service performs the necessary business logic and makes use of the appropriate DAO for data-store access. Each DAO performs the necessary interactions with Hibernate in order to act upon a given data store. The UserDAO interface defines the methods each DAO must implement.

The UserDAO Interface

package com.jeffhanson.datatier;import com.jeffhanson.businesstier.model.UserInfo;public interface UserDAO{   public UserInfo createUser(String id,                              String fullName,                              String address,                              String city,                              String state,                              String zip)      throws DAOException;   public UserInfo readUser(String id)      throws DAOException;   public UserInfo[] readUsersByState(String state)      throws DAOException;   public void updateUser(UserInfo userInfo)      throws DAOException;   public void deleteUser(UserInfo userInfo)      throws DAOException;}

An implementation of the UserDAO interface enables access to the UserInfo object’s data store, and is provided by the HibernateUserDAO class (see Listing 4).

Closing the Gap
The architectural differences between Java object hierarchies and relational database tables make the task of persisting Java object data to and from relational databases quite daunting for developers. The “impedance mismatch” between relational tables and Java object hierarchies has led to the development of several different object-persistence technologies attempting to close the gap between the relational world and the object-oriented world. The Hibernate framework defines an object/relational mapping mechanism and query language that makes storage and retrieval of Java objects to and from a data store a relatively simple proposition.

devx-admin

devx-admin

Share the Post:
USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted