|Home > Documentation > Migrating to Apache Geronimo > Migrating from JBoss to Geronimo > JBoss to Geronimo - Hibernate Migration|
This article will help you migrate applications using Hibernate 3.2 as the ORM tool from JBoss Application Server 4.2.1 to Apache Geronimo 2.0.
Hibernate is a powerful, high performance object/relational persistence and query service. It helps in the development of persistent (POJO) classes which have the getter and setter methods for attributes that are then mapped to fields in the database. You may follow object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.
Basically, Hibernate maps the Java classes to the database tables. It also provides the data query and retrieval facilities that significantly reduce the development time. It fits in naturally to the object-oriented mode of code development in the JAVA middle-tier. A unique feature of Hibernate is that it facilitates transparent persistence that enables the applications to switch to any database be it MySQL, Oracle or DB2. Hibernate can be used in Java Swing applications, Java Servlet-based applications, or J2EE applications using EJB session beans (Java Servlet based application in our case).
In order to illustrate the steps involved in the migration a sample application is provided which we will first deploy in JBoss and then migrate to Geronimo. The sample application will be the Online Brokerage Application which we already used in the JDBC migration example. This application has been changed to use Hibernate for persistence.
This article is organized in the following sections:
Hibernate can provide the following services:
It is also very flexible and we can use any combinations of these services. In this article we will be configuring hibernate to provide only the O/R Mapping feature while JBoss/Geronimo will provide both Transaction Management and Connection Management. This is the most commonly used configuration of Hibernate with an application server. Hibernate requires a configuration file
hibernate.cfg.xml which creates the connection pool and sets up the required environment. It contains parameters like the database driver, connection url, dialect (this specifies which RDBMS is being used), username, password, pool size among others. It also contains the location of the Mapping File
*.hbm.xml. A Mapping File is used to map the fields in the database table to the persistent class attributes.
These properties are common to any application server including Apache Geronimo v2.0.
However, JBoss (more specifically the Hibernate MBean) provides two additional deployment mechanisms.
The first is the Hibernate Archive (HAR file). Here, all the Hibernate classes and mapping files are packaged in a special archive, the HAR file. JBoss deploys this archive in the same way as it does with an EAR or a WAR file.
The alternative to the HAR file is to simply put all the Hibernate classes and mapping files with the other application classes, like in an EAR for example. The Hibernate MBean would be separately configured and told to search all application JARs for mapping files. Both deployment mechanisms allow you to provide Hibernate objects to your application code without performing any manual configuration or writing of the setup code normally required.
Structurally, a HAR file resembles a JBoss service archive (SAR file). The HAR file contains the Hibernate class files and the mapping files (*.hbm.xml) along with the standard
jboss-service.xml file containing a definition for the Hibernate MBean configured for the needs of the Hibernate application being created. In the latest JBoss distribution, it has been renamed as
hibernate-service.xml, but it retains the same structure and purpose.
Hibernate archives can be deployed as top-level packages or as a component of an EAR file. Since Hibernate archives are not a standard J2EE deployment type, we need to declare them in the
jboss-app.xml file of an EAR file to use them in that context.
The following table provides a feature-to-feature comparison between these two applicaiton servers.
Apache Geronimo v2.0
Supported. Hibernate is able to use a datasource given its JNDI name. This is because it is running in the same thread as the application.
Supported. Hibernate can lookup a datasource from JNDI given its JNDI name.
Automatic JNDI binding
Supported. Once the property is set the session factory is bound to the JNDI context.
JTA Session binding
This feature is not supported out of the box. We need to write a lookup for the Geronimo Transaction Manager to enable this.
Supported out of the Box. Hibernate provides a lookup class for the JBoss Transaction Manager.
Not Supported out of the box. Can be implemented by writing a GBean and a Hibernate Connection Provider class.
Supported. Hibernate is distributed with
Hibernate Archive (HAR)
Not Supported. Hibernate classes are deployed as a part of the J2EE archives.
Supported. A HAR packages the configuration and mapping files enabling extra server support to deployment.
You can use caching mechanisms provided by hibernate.
You can use caching mechanisms provided by hibernate. Integration with JBoss Cache is also supported.
Not Supported. It is required to manually open sessions. Only the transaction needs to be closed.
The Hibernate Session's lifecycle can be automatically bound to the scope of a
Hibernate Mapping Files
We need to specify the locations of the Hibernate mapping files.
If we use HAR deployment JBoss will automatically lookup the Hibernate mapping files.
This article contains a sample application to demonstrate migrating an application from JBoss to Geronimo, called Online Brokerage . It represents an online trading scenario in which users can buy and sell stocks. The application has the following five pages:
The following figure illustrates the application flow:
First, the user accesses the Login page. From the Login page the user enters the user name and password. If the user name or password is not valid the application throws an error message and rejects the user's login attempt. If the user name and password are correct, the user is taken to the Available Stocks page where he/she can view all the stocks that are present for sale at that time.
The user can choose to buy as many stocks as wanted, depending on the available money in the account, by clicking the Buy button. After the transaction completes successfully the user is brought back to the Available Stocks page where he/she can buy more stocks if required. If the user has insufficient funds to buy stocks the application will throw an error and will not process the transaction. The error message is shown at the top of the Available Stocks page. There is a User Info button on this page. By clicking this button the user is taken to the User Info page and shown the user details.
New users can register by clicking the Register button in the login page. In the Registration page the user will enter a user id, user name, password, address and available cash.
Back to Top
The Online Brokerage sample application consists of the following packages:
The Online Brokerage also includes the following JSP pages:
The tools used for developing and building the sample application are:
Ant is a pure Java build tool. It is used for building the war files and populating the database for the Online Brokerage application. Ant can be downloaded from the following URL:
At the time of writing this article, Hibernate 3.2 is the latest version available and can be downloaded at the following URL:
Additional documentation on Hibernate may be found at the following URL:
Download and install Hibernate, the installation directory will be later referred as <hibernate_home>.
The database used for demonstrating this application is MySQL. The sample database name is adi and it consists of the following three tables, STOCKS, USERS and USERSTOCKS. The fields for each of these tables are described below.
ID (PRIMARY KEY)
USERID (PRIMARY KEY)
ID (PRIMARY KEY)
The USERSTOCKS table is used to store the stocks that are owned by each user. The USERS and STOCKS tables are used to store the user and stock details. Because this is just a sample application the amount of money that a user has is entered during user registration by the user itself.
The DDL file used for populating this database is db.sql and it is located in the <brokerage_home>\sql directory.
This section shows you how and where the sample JBoss reference environment was installed so you can map this scenario to your own implementation.
Detailed instructions for installing, configuring, and managing JBoss are provided in the product documentation. Check the product Web site for the most updated documents.
The following list highlights the general tasks you will need to complete to install and configure the
initial environment as the starting point for deploying the sample application.
run.sh -c <your_server_name>command from the <jboss_home>\bin directory.
In order to build and run the Library application included in this article, you need to install and configure the build tool and the database that is used by the application.
As mentioned earlier in the article, this application is using the MySQL database that can be downloaded from the following URL:
Note: The appropriate version of the MySQL Connector/J should also be downloaded (3.1, 5.0, or 5.1), depending on your MySQL version.
This MySQL connector must also be placed in <JBOSS_HOME>/server/default/lib.
The Installation and configuration of MySQL is fairly intuitive and it is well documented in the MySQL Reference Manual available at the following URL:
Note: During the instance configuration I modified the security settings and specified "password" as the password for the root user. This user ID and password will later be used for accessing the database from the sample application.
Once the MySQL instance is configured you need to create the sample database that will be used by the Library application. From a command line, type the following command to start the MySQL monitor:
mysql -u root -ppassword
Note that there is no blank between the flag -p and the password.
This will bring up the MySQL command interface as shown in the following example:
From the MySQL command interface create the adi sample database by typing the following command:
mysql> create database adi;
As mentioned before, Apache Ant is used to build the binaries for the Online Brokerage application. If you do not have Ant installed this is a good time for doing it and make sure that <ant_home>\bin directory is added to the system's path variable.
Apache Ant can be downloaded from the following URL:
JBoss comes bundled with Hibernate so there is no need to download separate jars for hibernate.
The Online Brokerage application included with this article provides an Ant script that you will use in order to build the application and populate the database. Download the sample application by cliking on Online Brokerage.
After extracting the zip file, a brokerage directory is created, throughout the rest of the article we will refer to this directory as <brokerage_home>. In that directory open the build.properties file and edit the properties to match your environment as shown in the following example:
Note that the path to the Hibernate jars defined by the
dependency.dir tag are necessary by Geronimo, JBoss comes with it's own copy of Hibernate. Still you will need to copy to that directory the hibernate3.jar file located in the <hibernate_home> directory.
Important: When specifying the paths in this file, make sure you use forward slash "/" otherwise you will get compilation errors.
From a command prompt or shell change directory to <brokerage_home> and run the ant command. This will build the war, har and ear files and place them in the <brokerage_home>\jboss-artefact directory. The war created by the ant build contains a JBoss specific deployment descriptor jboss-web.xml file in the WEB-INF directory of the WAR. The HAR contains a JBoss specific hibernate-service.xml file in the META-INF directory and the EAR contains a JBoss specific deployment descriptor jboss-app.xml. These files are shown below.
The resource-ref element is used to map the resource referred to by the name jdbc/HibernateDB in the web.xml file to the resource with the JNDI name java:jdbc/ HibernateDS, for example MySQL datasource.
This file contains the hibernate properties that need to be set. The property names and their functions are:
The hibernate-service.xml file should be present in the META-INF directory of the EAR.
The jboss-app.xml file located in the META-INF directory of the EAR specifies the name of the har file present in the EAR.
The following example shows the deployment descriptor web.xml used in this application. The web.xml file located in the WEB-INF directory of
brokerage.war is the deployment descriptor where you specify, among other things, the servlets name and default JSP for the application.
As we mentioned before, the db.sql script is provided to populate the sample data. The location of this file is already defined in the build.properties by the tag sql.file. To populate the sample data simply run the following command from the <brokerage_home> directory.
Before you deploy the sample application you need to configure the datasource required by this application. To deploy the datasource configuration in JBoss copy the mysql-ds.xml file located in the <brokerage_home>\plan directory to the following directory:
Similarly to the database deployment, to deploy the Online Brokerage application in JBoss, copy the brokerage.ear file you just built with Ant to the following directory:
If JBoss is already started, it will automatically deploy and start the application; otherwise, the application will be deployed and started at the next startup.
To test the application, open a Web browser and access the following URL:
This brings up the login screen of the Online Brokerage application. Enter j2ee as the user name and password as the password and click on login. This takes you to the available stocks page illustrated in the following figure. The application is now configured and running.
Back to Top
Download and install Geronimo from the following URL:
The release notes available there provide clear instructions on system requirements and how to install and start Geronimo. Throughout the rest of this article we will refer to the Geronimo installation directory as <geronimo_home>.
For running the Online Brokerage application in Geronimo, you will be using the same MySQL database that was used with JBoss. So the first task you need to do in order to prepare the Geronimo environment is to configure the data source.
You need to copy the MySQL database driver into the Geronimo repository so that you can refer to it in the data source deployment plan. The Geronimo repository is located in the <geronimo_home>/repository directory. Inside this directory, create a new directory called mysql/jars and copy the mysql-connector-java-3.1.14-bin.jar file into it.
Now, you need to define a data source deployment plan. For your convenience, the sample application already provides a deployment plan called mysql-geronimo-plan.xml located in the <brokerage_home>\plan directory. The following example shows the content of this deployment plan.
Now that you have a deployment plan and have copied the drivers the next step is to acutally deploy the datasource connection pool. If you have not yet started Geronimo please do so by running the following command:
To deploy the datasource connection pool run the following command:
<geronimo_home>\bin\deploy --user system --password manager deploy <brokerage_home>\plan\mysql-geronimo-plan.xml ..\repository\org\tranql\tranql-connector-ra\1.3\tranql-connector-ra-1.3.rar
Depending on your environment you should see a confirmation message similar to this one:
Apache Geronimo does not support HAR archives so we will be having all the classes inside a WAR archive. We need to write two classes for making the application work on Geronimo. One is a TransactionManagerLookup class for Geronimo and the other is a utility class HibernateUtil to get the SessionFactory. In addition to this we will need to make some changes to the TradeDispatcherServlet and TradeDAO classes.
As a first step we will see the changes that need to be made to the application as illustrated in the following list.
In JBoss the HibernateMBean will create and bind the SessionFactory to the Global JNDI Context. So we can obtain the SessionFactory through a simple lookup. We can then get the session from the SessionFactory.
TradeDAO.java is located in the
In Geronimo we will write a new utility class to create the SessionFactory, this class will be the HibernateUtil class. It has a method getCurrentSession() that will return the session.
Modify the first part of
TradeDAO.java to match the changes in the following excerpt.
Do a search and replace, look for the following string:
and replace it with
You should count 9 replacements.
This difference in obtaining the session will be the main change in the codebase for the application to run in Apache Geronimo.
As mentioned above we will use this class to create the SessionFactory and provide the application with Hibernate sessions. The source code for the class is given below
HibernateUtil.java is located in the
<brokerage_home>/src/com/dev/trade/util directory. For your convenience, a copy of this file is already provided with the sample application.
In this class there will also be a difference in getting the SessionFactory. In JBoss we get it from the JNDI Context but in Geronimo we will get it through the utility class.
TradeDispatcherServlet.java is located in the
<brokerage_home>/src/com/dev/trade/servlet directory. Replace the doGet method for the one shown in the following example.
Hibernate comes with transaction manager lookup classes for many application servers. Unfortunately Hibernate 3.2 does not have a lookup class specific for Apache Geronimo, so we need to write our own. The code for the Geronimo specific transaction manager lookup is shown in the following example.
For your convenience this class is already provided in the
<brokerage_home>/TransactionManager directory. Create the following directory structure and copy the
Now you need to create the hibernate configuration file hibernate-cfg.xml. Inside this file you will specify the required hibernate configuration attributes. Note that the
hibernate-service.xml in JBoss is for doing the same thing. For your convenience this file is also provided with the sample application in the
Additional details for the specific functions of each of these properties can be found in the hibernate manual.
One last step before building is to create a geronimo-web.xml file which is the Geronimo specific deployment descriptor as illustrated in the following example. Once again, for your convenience this file is also provided with the sample application in the
<hidden-classes> element is for preventing certain classes that come with Apache Geronimo from being visible to the war classloader which may result in version problems.
To build the application run the following commands
GeronimoTransactionManagerLookupclass. If you have not downloaded the hibernate source you can compile the class after putting the hibernate jar in the classpath and then manually add that class to the hibernate jar file.
org\hibernate\transactiondirectory in the
Note that we are just building the war target because the Online Brokerage sample application is just a web application. In the JBoss section we used an ear to package the war and the har, this last one is not supported by Apache Geronimo.
This will create a brokerage.war file in the <brokerage_home>/geronimo-artefact directory.
The <brokerage_home>/solutions directory has the source files already migrated to compile for and run in Apache Geronimo.
To deploy the migrated Online Brokerage application, make sure the Geronimo server is up and running and run the following command:
deploy --user system --password manager deploy <brokerage_home>/geronimo-artefact/brokerage.war
Once the application is deployed, open a Web browser and access the following URL:
Login with the same user name and password you used when testing the application from JBoss.
This article has shown you how to migrate a sample application that uses Hibernate as its O/R mapping layer, from JBoss to the Apache Geronimo application server. Some of the features/functionalities already provided by JBoss may not yet be implemented in Geronimo, as a consequence some minor additional coding may be required but the overall migration complexity is low.