Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Simplify Java Object Persistence with Hibernate : Page 4

The 'impedance mismatch' between relational databases' tabular orientation and object-oriented Java's hierarchical one is a perennial problem for which the Java world has several good solution offerings. The second in a three-part series, this article examines the ways that the Hibernate framework attempts to simplify the chore of connecting relational databases and the Java programming language.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

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 <?xml version="1.0" encoding="ISO-8859-1"?> <jaws> <datasource>java:/DefaultDS</datasource> <type-mapping>Hypersonic SQL</type-mapping> </jaws>

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 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:data/userejb</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="show_sql">true</property> <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property> <property name="transaction.factory_class"> net.sf.hibernate.transaction.JDBCTransactionFactory </property> <property name="hibernate.cache.provider_class"> net.sf.hibernate.cache.HashtableCacheProvider </property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/jeffhanson/businesstier/model/UserInfo.hbm.xml"/> </session-factory> </hibernate-configuration>

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.

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <!-- com.jeffhanson.businesstier.model.UserInfo root --> <class name="com.jeffhanson.businesstier.model.UserInfo" table="USEREJB"> <id name="id" type="string"> <column name="id" length="16"/> <generator class="uuid.hex"/> </id> <property name="address" column="address" type="string"/> <property name="zip" column="zip" type="string"/> <property name="state" column="state" type="string"/> <property name="city" column="city" type="string"/> <property name="fullName" column="fullName" type="string"/> </class> </hibernate-mapping>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date