Java Reference
In-Depth Information
Annotations and Enterprise JavaBeans 3
The newly released Enterprise JavaBeans 3 (EJB 3) specification includes a mandatory ORM
component. Hibernate's design influenced many of the changes (including lightweight per-
sistence and the query language). The configuration requirements for Hibernate in an EJB 3
environment are somewhat different from the default requirements—we discuss this in more
depth in Appendix A. If you have used EJB 1.x or 2.x in the past, you will find EJB 3 to be a
much-needed simplification.
Hibernate 3 permits you to take advantage of the new Annotations feature of Java 5. These
annotations can be used in conjunction with, or in place of, some of the configuration files
that previous versions of Hibernate demanded. These annotations are essentially an EJB 3 fea-
ture, although Hibernate supplies some additional proprietary extensions. In Chapter 6 we
discuss how to use these persistence annotations.
JMX and Hibernate
JMX is a standard API for managing Java applications and components—mostly accessed
through MBeans, which represent wrappers for services and resources. Hibernate provides
two MBeans for JMX: HibernateServiceMBean and StatisticsServiceMBean . Both of these are
interfaces that reside in the org.hibernate.jmx package. The HibernateService and
StatisticsService classes implement the interfaces and reside within the same package. The
HibernateServiceMBean provides getter and setter methods for many of the Hibernate configu-
ration properties, including the data source, transaction strategy, caching, dialect, and other
database options. It also provides a mechanism for adding mapping files to the configuration.
When the HibernateServiceMBean starts, it creates a Configuration object from its properties
and mapping files, and then builds a SessionFactory object. The SessionFactory object binds
to the JNDI location specified on the JMX MBean, and your Java application can then use
standard JNDI calls to obtain the session factory.
The other MBean supplies statistics. Hibernate can log statistics about the performance
of query execution, caching, and object entity operations. Using a JMX console, an adminis-
trator can enable statistics and then access up-to-date performance indicators through the
console.
The advantage of JMX over programmatic access to these features is that administrators
or other non-developers may change properties at run time through a standardized JMX con-
sole that is independent of Hibernate and applies to a range of other frameworks and
components.
Hibernate Configuration
Before you create a session factory, you must tell Hibernate where to find the mapping infor-
mation that defines how your Java classes relate to the database tables. Hibernate also requires
a set of configuration settings, which are usually supplied as a standard Java properties file
called hibernate.properties , or as an XML file named hibernate.cfg.xml .
We recommend using the XML format. This allows you to specify the location of the
mapping information from the configuration files—the alternative (when using properties
files) being to programmatically supply this information to Hibernate through the
Configuration class.
Search WWH ::




Custom Search