Exercise 8.2: Single Table per Hierarchy




Exercise 8.2: Single Table per Hierarchy

This exercise maps the Person-Customer-Employee hierarchy using the TABLE_PER_CLASS inheritance mapping strategy.

Start Up JBoss

If you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in Workbook 1.

Initialize the Database

The database tables will be created when Exercise 8.2 is deployed to JBoss. If you have problems running this example, shut down JBoss and run the clean.db Ant task.

Build and Deploy the Example Programs

Perform the following steps:

  1. Open a command prompt or shell terminal and change to the ex08_2 directory created by the extraction process.

  2. Set the JAVA_HOME and JBOSS_HOME environment variables to point to where your JDK and JBoss 4.0 are installed. Examples:


    Windows:

    C:\workbook\ex08_2> set JAVA_HOME=C:\jdk1.5.0
    C:\workbook\ex08_2> set JBOSS_HOME=C:\jboss-4.0.x
    


    Unix:

    $ export JAVA_HOME=/usr/local/jdk1.5.0
    $ export JBOSS_HOME=/usr/local/jboss-4.0
    

  3. Add ant to your execution path. Ant is the build utility.


    Windows:

    C:\workbook\ex08_2> set PATH=..\ant\bin;%PATH%
    


    Unix:

    $ export PATH=../ant/bin:$PATH
    

  4. Perform the build by typing ant.

As in the last exercise, titan.jar is rebuilt, copied to the JBoss deploy directory, and redeployed by the application server.

Examine the Database Schema

Now that you have built the example, look at the database schema by going to the JMX management console and bringing up the Hypersonic Database Manager described in previous chapters. This will allow you to see how the code in Chapter 8 maps to an autogenerated database (see Figure W-14).

Figure W-14. TABLE_PER_CLASS Schema


The Hypersonic Database Manager is particularly useful in the exercises in this chapter because the code is basically the same, but the schema is different for each example.

Examine the Code

The code for this exercise is exactly the same as Exercise 8.1, except that the entity classes are mapped using the TABLE_PER_CLASS inheritance strategy and there is one minor change to the DataAccessBean class. We do not use auto-primary-key generation for this example. The default generation strategy Hibernate uses for the Hypersonic SQL database engine is the IDENTITY strategy. This generation technique is not compatible with the SQL UNION s that are needed to perform the mappings internally.

Run the Client

Run the Client application by invoking ant run.client at the command prompt. Remember to set your JBOSS_HOME and PATH environment variables. This is the output:

run.client:
     [java] persons.size( ) = 3
     [java]     class is: com.titan.domain.Person
     [java]     person: Bill Burke
     [java]     class is: com.titan.domain.Customer
     [java]     person: Sacha Labourey
     [java]     class is: com.titan.domain.Employee
     [java]     person: Gavin King