Hibernate Archives


JBoss provides a new, simplified way to package and deploy Hibernate applications. JBoss introduces the notion of a HAR file. The HAR file allows you to provide Hibernate objects to your application code without performing any of the tedious manual configuration or setup code normally required.

Structurally, a HAR file resembles a JBoss SAR file. The HAR file contains the Hibernate class files and mapping files, along with a hibernate-service.xml deployment descriptor in the META-INF directory. We'll look first at the hibernate-service.xml file.

The hibernate-service.xml file is actually just a normal jboss-service.xml file. The name is different, but the content is the same. The file should contain a definition for the Hibernate MBean (see the section "The Hibernate MBean," earlier in this chapter) configured correctly for the needs of the Hibernate application being created. The following example shows a typical hibernate-service.xml file:

[View full width]

<server> <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate"> <attribute name="DatasourceName">java:/DefaultDS</attribute> <attribute name="Dialect">net.sf.hibernate.dialect.HSQLDialect</attribute> <attribute name="SessionFactoryName">java:/hibernate/CaveatEmptorSessionFactory< /attribute> <attribute name="CacheProviderClass">net.sf.hibernate.cache.HashtableCacheProvider< /attribute> <attribute name="Hbm2ddlAuto">create-drop</attribute> <attribute name="ShowSqlEnabled">true</attribute> </mbean> </server>

Notice that the hibernate-service.xml file does not contain a list of the classes Hibernate is to map the way a hibernate.cfg.xml file would, and you do not need to manually add the Hibernate mapping files to the configuration the way you would when using a hibernate.properties file. Instead, the Hibernate deployer in JBoss scans the archive for .hbm.xml mapping files at deployment time and adds them to the configuration for you. Figure 13.1 shows the layout of a typical HAR file.

Figure 13.1. The structure of a typical HAR file.


The Hibernate deployer creates a configuration, using all the .hbm.xml files in the archive, and binds a Hibernate SessionFactory into the JNDI tree at the appropriate location. The SessionFactory can then be used by any application in JBoss. This example comes from the CaveatEmptor demo (http://caveatemptor.hibernate.org/). The CaveatEmptor code is provided along with the examples in this book. To deploy the Hibernate portion of the demo as a HAR file, you run the following command:

 [examples]$ ant -Dchap=hibernate -Dex=1 run-example ... [copy] Copying 1 file to /tmp/jboss-4.0.1/server/default/deploy 

This creates and deploys the HAR file shown in Figure 13.1. If you look at the console log, you will see the Hibernate classes located and deployed as well as the results of the schema generation.



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net