Chapter 9 Generating Persistent Objects for Java and JSF Pages


Defining primary identifier mappings

Primary identifier mapping is mandatory in Hibernate. Primary identifiers of classes are mapped to primary keys of master tables in data sources. If not defined, a default primary identifier mapping will be generated, but this may not work properly.

There are three kinds of primary identifier mapping in Hibernate:

Mapped classes must declare the primary key column of the database table. Most classes will also have a Java-Beans-style property holding the unique identifier of an instance.

Simple identifier mapping

When a primary key is attached to a single column, only one attribute in the primary identifier can be mapped. This kind of primary key can be generated automatically. You can define the generator class and parameters. There are many generator class types, such as increment, identity, sequence, etc. Each type of generator class may have parameters that are meaningful to it. See your Hibernate documentation for detailed information.

You can define the generator class and parameters in the Hibernate tab of the primary identifier property sheet. The parameters take the form of param1=value1; param2=value2.


Steps To define an identifier mapping:

  1. Open the class property sheet and click the Attributes tab.
  2. Create an attribute and set it as the Primary identifier.
  3. Click the Identifiers tab and double-click the entry to open its property sheet.
  4. Click the Hibernate tab, select a generator class and define its parameters.

    Example parameters:

  5. You can check the code in the Preview tab:


    Note that, if there are several Primary identifier attributes, the generator is not used.

Composite identifier mapping

If a primary key comprises more than one column, the primary identifier can have multiple attributes mapped to these columns. In some cases, the primary key column could also be the foreign key column.

Steps To define a composite identifier mapping:

  1. Define association mappings.
  2. Migrate navigable roles of associations.
  3. Add these migrated attributes in primary identifier. The migrated attributes need not to be mapped.


In the above example, the Assignment class has a primary identifier with three attributes: one basic type attribute and two migrated attributes. The primary identifier mapping is as follows:

<composite-id>
    <key-property name="type">
        <column name="type" sql-type="smallint" 
            not-null="true"/>
        </key-property>
        <key-many-to-one name="title">
        </key-many-to-one>
        <key-many-to-one name="worker">
        </key-many-to-one>
</composite-id>

Component primary identifier mapping

For more convenience, a composite identifier can be implemented as a separate value type class. The primary identifier has just one attribute with the class type. The separate class should be defined as a value type class. Component class mapping will be generated then.

Steps To define component primary identifier mapping:

  1. Define a primary identifier attribute.
  2. Define the type of the attribute as a value type class.
  3. Set the Class generation property of the primary identifier attribute to Embedded.
  4. Set the ValueType of the primary identifier class to true.
  5. Define a mapping for the primary identifier class.


In the example above, three name attributes are grouped into one separate class Name. It is mapped to the same table as Person class. The generated primary identifier is as follows:

<composite-id name="name" class="identifier.Name">
    <key-property name="firstName">
        <column name="name_firstName"
            sql-type="text"/>
    </key-property>
    <key-property name="middleName">
        <column name="name_middleName" 
            sql-type="text"/>
    </key-property>
    <key-property name="lastName">
        <column name="name_lastName" 
            sql-type="text"/>
    </key-property>
</composite-id>

Note: The value type class must implement the java.io.Serializable interface, which implements the equals() and hashCode() methods.

 


Copyright (C) 2007. Sybase Inc. All rights reserved.