Generated Schema
The application in this chapter is intended to generate the database schema from the mapping files, generated in turn from the Java source. The representation of the Artifact object in a database is fairly straightforward, as shown in Listing 3.9.
Listing 3.9. Artifact Table Definition
mysql> desc artifact;
+----------------+--------------+------+-----+---------+------+
| Field | Type | Null | Key | Default | Extra
|
+----------------+--------------+------+-----+---------+------+
| id | bigint(20) | | PRI | NULL |
auto_increment |
| comments | varchar(255) | YES | | NULL | |
| dateCreated | datetime | YES | | NULL | |
| dateDiscovered | datetime | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| location | varchar(255) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
+----------------+--------------+------+-----+---------+------+
7 rows in set (0.00 sec)
The representation of the Owner class and the related subclasses, however, is a bit more complex, as shown in Listing 3.10.
Listing 3.10. Owner Table Definition
mysql> desc owner;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra
+---------------+--------------+------+-----+---------+-------+
| id | bigint(20) | | PRI | NULL | auto_
increment |
| discriminator | varchar(255) | | | | |
| name | varchar(255) | YES | | NULL | |
| dateCreated | datetime | YES | | NULL | |
| location | varchar(255) | YES | | NULL | |
| owner | bigint(20) | YES | MUL | NULL | |
| endDate | datetime | YES | | NULL | |
| startDate | datetime | YES | | NULL | |
| dateOpened | datetime | YES | | NULL | |
| birthDate | datetime | YES | | NULL | |
| deathDate | datetime | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
Looking at the owner table in Listing 3.10 more closely, several interesting details emerge. The discriminator column is used to store the class type information regarding the record. Listing 3.11 shows an example of several records stored in the owner table; note the class information.
Listing 3.11. Sample Owner Records
mysql> select id, discriminator, name from owner;
+----+-------------------------------+-------------------------+
| id | discriminator | name |
+----+-------------------------------+-------------------------+
| 1 | com.cascadetg.ch03.Person | Smith, Bob |
| 2 | com.cascadetg.ch03.Foundation | Smith Foundation |
| 3 | com.cascadetg.ch03.Museum | Waldendorf |
| 4 | com.cascadetg.ch03.Exhibition | New Acquisitions 2005 |
+----+-------------------------------+-------------------------+
4 rows in set (0.00 sec)
Any column not relevant to a particular subclass is set to null. For example, a Person has no date-opened property, and therefore the dateOpened column for Person records is set to null.
Hibernate automatically manages the discriminator for you as you work with persistent objects. For example, when Hibernate retrieves the four records shown in Listing 3.11, it will automatically instantiate the proper object and set the values for you, returning a set of Owner objects with the proper underlying class.
In addition to the expected two tables, Hibernate also uses a third table to manage the relationships between artifacts and their owners, as shown in Listing 3.12. This table isn't mapped to a particular Java class; it is instead implied by the bi-directional set references on both Owner and Artifact. In database terminology, this is a many-to-many relationship. For more information on the link between a set collection and a many-to-many database relationship, see Chapter 7.
Listing 3.12. Ownership Table
mysql> desc ownership;
+-------------+------------+------+-----+---------+------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+------------+
| owner_id | bigint(20) | | PRI | 0 | |
| artifact_id | bigint(20) | | PRI | 0 | |
+-------------+------------+------+-----+---------+------------+
2 rows in set (0.00 sec)
|