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.

jboss hibernate tutorial

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....)

hibernate jboss tutorial

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.)

hibernate jboss tutorial

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:

jboss hibernate tutorial

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.

jboss hibernate tutorial

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.


jboss hibernate tutorial

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.
Adding Hibernate configuration to your project

The advantage of embedding the Hibernate application in JBoss AS is that you can expose Hibernate SessionFactory through a JNDI tree and modify its configuration at runtime.

This is indeed a great configuration advantage; before the new release of JBoss AS, you had to delegate to an MBean the creation of the Hibernate SessionFactory and its exposure through JNDI.

For example, if you wanted to configure a SessionFactory at the naming context hibernate/SessionFactory, you would have to package your Hibernate application with a file named xxx-service.xml in the META-INF folder. Here's a sample of it:

<server>
  <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
  <attribute name="DatasourceName">java:/ MySqlDS</attribute>
  <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute>
  <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
  <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
 </mbean>
</server>


This configuration is still valid for pre 5.0 releases of JBoss AS. With the introduction of the new Virtual Deployment Framework (VDF), you now have to provide your SessionFactory configuration using the Hibernate XML schema. For example, if you want to link your SessionFactory to your MySQL database, you have to add the following service-hibernate.xml. (Be aware, the suffix is -hibernate.xml and not –service.xml.)

<hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
  <session-factory name="java:/hibernate/SessionFactory" bean="jboss.test.har:service=Hibernate, testcase=TimersUnitTestCase">
    <property name="datasourceName">java:/MySqlDS</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <depends>jboss:service=Naming</depends>
        <depends>jboss:service=TransactionManager</depends>
  </session-factory>
</hibernate-configuration>


The preceding configuration file needs to be stored in the META-INF folder of your Hibernate archive (HAR) file. The structure of the updated project from the Package Explorer is as shown in the following snapshot:

jboss hibernate tutorial

In the next part we will a web client to the project, package and deploy the application, generate EJB 3 using the wizard and compare the Hibernate framework with the JPA persistence standard, showing how the two technologies can be coupled in a single application.

 

This article has been taken from:

JBoss AS 5 Development

jboss as 5 development bookDevelop, deploy, and secure Java applications on this robust, open source application server

  • A complete guide for JBoss developers covering everything from basic installation to creating, debugging, and securing Java EE applications on this popular, award-winning JBoss application server
  • Master the most important areas of Java Enterprise programming including EJB 3.0, web services, the security framework, and more
  • Starts with the basics of JBoss AS and moves on to cover important advanced topics with the help of easy-to-understand practical examples
  •  Written in a very simple and readable style, this book includes essential tips and tricks that will help you master JBoss AS development


Read the second part of this chapter here

0
0
0
s2smodern