JBoss AS 5 Development
The JBoss Application Server is a Java EE-certified platform for developing and
deploying Java Enterprise applications. JBoss Application Server provides the full
range of J2EE 1.5 features as well as extended Enterprise services including clustering,
caching, and persistence. This book will show Java EE developers how to develop their
applications using the JBoss Application Server. It covers topics such as:
- Setting up a development environment
- Customization
- Java EE programming modules
- Clustering
- Security
All these features will be explored by developing sample and intuitive applications built
using the friendly interface of Eclipse and JBoss Tools.
What This Book Covers
Chapter 1: Installing Core Components covers the installation of the key components that
will be needed throughout the rest of the book. The installation process will be completed
by using intuitive wizards that will lead even inexperienced users through it.
Chapter 2: What's New in JBoss AS 5.0 introduces the reader to the most significant
changes brought by release 5.0 of the application server. The new server directory tree is
analyzed in detail and possible variants in the server configuration are discussed in the
latter part of this chapter.
Chapter 3: Customizing JBoss Services discusses the core configuration of the application
server. The highlights of it include an introduction to JBoss AS monitoring services, the
inner details about JBoss thread pool, how to configure logging services, and a detailed
description of the transaction and Datasource service.
Chapter 4: Developing EJB 3 Session Bean introduces the reader to some concrete Java
EE programming examples developed on JBoss AS 5. The focus of this chapter is on EJB
3 session Beans, including a section about their configuration for optimal results.
Chapter 5: Developing JPA Entities covers the development of an example based on the
Java Persistence API (JPA). Here, we introduce an enterprise application named the
Appstore, which will be a central theme of this book.
Chapter 6: Creating a Web Application is about developing and configuring web
applications on JBoss AS 5.0 using the JSF cutting-edge technology. In the first part of
this chapter we will enhance the Appstore Enterpirse application by adding a web layer to
it. In the latter part, we explain in detail how to properly configure JBoss Web Server.
Chapter 7: Developing Applications with JBoss Messaging Service discusses JBoss
Messaging provider by giving a short introduction to the new messaging system. The
chapter then helps us set up some proof of concept programming examples.
Chapter 8: Developing Applications with JBoss and Hibernate covers the de facto
standard object relational mapping tool, Hibernate, showing how to quickly set up a
Hibernate project using the facilities provided by the JBoss tools interface.
Chapter 9: Managing JBoss AS covers the Java Management Extension (JMX),
which still plays a vital role in the application server infrastructure. The chapter
includes many examples that show how to write traditional MBeans services and the
new POJO Services.
Chapter 10: Developing Applications with JBoss Web Services focuses on the JBoss
Web Service implementation, JBossWS, showing how to create, deploy, and test Web
Services on JBoss AS along with some advanced concepts such as Handler chains and
SOAP debugging.
Chapter 11: Clustering JBoss AS covers the facts about JBoss AS clustering
configuration, moving from cluster basics to detailed configuration of the individual
services of the application server.
Chapter 12: Developing a Clustered Application continues the journey in the clustering
arena by adding some concrete examples based on the abstract concepts covered in the
earlier chapter.
Chapter 13: JBoss AS Security provides a systematic guide to JBoss security framework
and the cryptographic interfaces available in the Java EE framework. This supplies the
basis for concrete examples, which are delivered in the next chapter.
Chapter 14: Securing JBoss AS Applications continues the in-depth exploration of the
JBoss security framework, adding concrete programming examples applied on the EJB
and Web Services technologies.
Developing Applications with JBoss and Hibernate
Hibernation is a state of regulated hypothermia undergone by some animals to
conserve energy during the winter. – Wikipedia
In this chapter, we will introduce Hibernate, which is the de facto standard
object-relational mapping framework for Java applications. The Hibernate galaxy
is quite large and needs a book of its own to be fully explored. Our mission will be
to take over one sector of this galaxy, especially where Hibernate applications are
managed by JBoss AS.
In this chapter, we will cover the following topics:
- A short introduction to Hibernate
- Setting up our proof of concept for the Hibernate project
- Reverse engineering a database schema into Hibernate POJOs and
mapping files
- Deploying the application to JBoss AS
- Comparing the Hibernate technology with EJB 3 persistence (JPA)
Introducing Hibernate
Hibernate provides a bridge between the database and the application by persisting
application objects in the database, rather than requiring the developer
to write and maintain lots of code to store and retrieve objects.
The main configuration file, hibernate.cfg.xml, specifies how Hibernate obtains
database connections, either from a JNDI DataSource or from a JDBC connection
pool. Additionally, the configuration file defines the persistent classes, which are
backed by mapping definition files.
This is a sample hibernate.cfg.xml configuration file that is used to handle
connections to a MySQL database, mapping the com.sample.MySample class.
<hibernate-configuration>
<session-factory>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="connection.url">
jdbc:mysql://localhost/database
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/sample/MyClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>
From our point of view, it is important to know that Hibernate applications can
coexist in both the managed environment and the non-managed environment. An
application server is a typical example of a managed environment that provides
services to hosting applications, such as connection pooling and transaction.
On the other hand, a non-managed application refers to standalone applications,
such as Swing Java clients that typically lack any built-in service.
In this chapter, we will focus on managed environment applications, installed on
JBoss Application Server. You will not need to download any library to your JBoss
installation. As a matter of fact, JBoss persistence layer is designed around Hibernate
API, so it already contains all the core libraries.
Creating a Hibernate application
You can choose different strategies for building a Hibernate application. For example,
you could start building Java classes and map files from scratch, and then let Hibernate
generate the database schema accordingly. You can also start from a database schema
and reverse engineer it into Java classes and Hibernate mapping files. We will choose
the latter option, which is also the fastest. Here's an overview of our application.
In this example, we will design an employee agenda divided into departments. The
persistence model will be developed with Hibernate, using the reverse engineering
facet of JBoss tools. We will then need an interface for recording our employees and
departments, and to query them as well.
The web interface will be developed using a simple Model-View-Controller (MVC)
pattern and basic JSP 2.0 and servlet features.
The overall architecture of this system resembles the AppStore application that has
been used to introduce JPA. As a matter of fact, this example can be used to compare
the two persistence models and to decide which option best suits your project needs.
We have added a short section at the end of this example to stress a few important
points about this choice.
Setting up the database schema
As our first step, we are going to create the necessary tables for our example. Launch
a MySQL client and issue the following DDL:
CREATE schema hibernate;
GRANT ALL PRIVILEGES ON hibernate.* TO 'jboss'@'localhost' WITH GRANT
OPTION;
CREATE TABLE `hibernate`.`department` (
`department_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`department_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`department_id`)
)
ENGINE = InnoDB;
CREATE TABLE `hibernate`.`employee` (
`employee_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`employee_name` VARCHAR(45) NOT NULL,
`employee_salary` INTEGER UNSIGNED NOT NULL,
`employee_department_id` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`employee_id`),
CONSTRAINT `FK_employee_1` FOREIGN KEY `FK_employee_1` (`employee_
department_id`)
REFERENCES `department` (`department_id`)
ON DELETE CASCADE
ON UPDATE CASCADE
)
ENGINE = InnoDB;
With the first Data Definition Language (DDL) command, we have created a
schema named Hibernate that will be used to store our tables. Then, we have
assigned the necessary privileges on the Hibernate schema to the user jboss
(created in Chapter 5, Developing JPA Entities).
Finally, we created a table named department that contains the list of company
units, and another table named employee that contains the list of workers. The
employee table references the department with a foreign key constraint.
A new Eclipse project
Now start Eclipse. You don't have a specific project for Hibernate applications,
so a utility project (that simply packs the classes in an archive) will be enough.
You can reach this option from the menu by going to New | Other | Java EE |
Utility Project.
Name the project HibernateProject and target it to JBoss AS 5.0 Runtime. You can
leave the default JBoss AS configuration and hit Finish.
Now, we are going to unleash the full potential of Hibernate tools. Select from the
menu New | Other | Hibernate | Hibernate Configuration File. The Hibernate
configuration contains all of the details for wiring your application to the database.
You will be asked for the name and the parent folder of the configuration file. Accept
the default hibernate.cfg.xml at the root of your project.
Next , insert the details of your Hibernate configuration. Choose a name for your
session factory, which will contain your MySQL connection facets. Remember to
check the fl ag Create a console configuration, so that the wizard will complete the
console configuration as the next step.
A console configuration describes how the Hibernate plugin should interact with
Hibernate and what configuration files (including the classpath) are needed to load
the POJOs, JDBC drivers, and so on. This step is required to make use of query
prototyping, reverse engineering, and code generation.
The console wizard will look at the current selection in the IDE and will try to
autodetect the settings, which you can approve or modify to suit your needs. For
example, you don't need to enter the Configuration file or the Property file if you
have just one in your project; Eclipse will select it automatically.
One important selection is the Type option that lets you choose between the Core
hibernate configuration (Java classes backed by mapping files), Annotations, or even
JPA annotations. We will leave the selected Core option.
Before clicking Finish, select MySQL (InnoDB) as Database dialect in the Options
tab. No other changes are required.
Now verify that you have successfully linked to Hibernate by switching to Hibernate
Configuration. This view will be composed by a tree of three objects: Configuration,
Session Factory, and Database. Choose Database and verify that it expands
correctly to show the database tables of your schema.
If you fail to browse the database schema, check that you have correctly set up your
Hibernate configuration.
Reversing your schema into Java classes
The next move will be reversing our database schema into Java classes and mapping
files. This powerful feature is available from the menu: File | New | Hibernate |
Hibernate Reverse Engineering file. You can place this file in a convenient
location for your project and choose a name for it. The default name proposed is
hibernate.reveng.xml, which looks rather the tile of another fiction movie
from G. Lucas.
On the next page, select your Console configuration and choose the tables that will
be included in your reverse engineering process. (Hint: You have to hit Refresh first
to show the database schema and then click Include....)
What Eclipse has just created for you is a file named hibernate.reveng.xml that
should resemble the following code snippet:
<hibernate-reverse-engineering>
<table-filter match-catalog="hibernate" match-name="department"/>
<table-filter match-catalog="hibernate" match-name="employee"/>
</hibernate-reverse-engineering>
If you are smart at noticing variations, you might have discovered a new icon in
your toolbar. This is your gateway to the reverse engineering process. (Notice:
this icon is visible only in the Hibernate Perspective, you will not be able to find
it anywhere else.)
Click on Hibernate's down arrow icon and select Hibernate Code Generation
Configurations. In the next dialog, you will first have to create a new Hibernate
Code Generation Configuration that will contain all the details of your reverse
engineering process. Click on the New button located in the left corner of the wizard.
Now, select your brand new configuration and carefully choose the following
options. First, wire the Console configuration to your project (HibernateProject).
Then, choose an output directory for your generated files. We would suggest you
to point to your src folder. (Be aware that existing files will be overwritten, that's
why I just said you have to be careful!)
Just below, you will find the checkbox Reverse engineer from JDBC Connection. If
enabled, the tools will reverse engineer the available database using the connection
information in the selected Hibernate Console configuration. Check this option and
enter the package name for the generated classes, which will be com.packtpub.
hibernate. Leave the other text fields to the defaults and move to the tab Exporters.
The Exporters tab menu is used to specify which type of code should be generated.
Each selection represents an Exporter that is responsible for generating the code,
hence the name.
In the upper area of the dialog, you will notice an interesting checkbox named
Generate EJB 3 annotations. We will return to this useful option later. At the moment,
what we need is just to check the Domain code and Hibernate XML Mappings
options, which will generate the Java POJOs and mapping files respectively.
It took a bit of time to complete all of these steps; however, now your Java classes
and configuration files are handy and waiting to be packaged.
|