Creating an identifier

Simple identifiers

An identifier is an attribute of a persistent class used to universally identify the persistent class in question. In most cases, this identifier will

be mapped as a Primary Key of the SQL model.


An identifier belonging to a persistent class

The following identifier properties can be defined in the Hibernate tab:

  • Persistent Name:
    Name of the SQL field to which the identifier will be mapped. If this field remains empty, the name of the SQL field is considered to be identical to the name of the identifier.

  • Persistent Type:
    The Hibernate type of the identifier.

  • Generator:
    The name of the generator associated with this identifier.

Generators

A generator is used to generate unique identifiers for the instances of persistent classes.

The “Generator” property of the identifier enables you to assign a generator type to the identifier. Certain generators require parameters. The

identifier’s “Parameters” property is used to enter these parameters, which must respect the following syntax: nameparam1, valueparam1 ; nameparam2, valueparam2 ; …

  • native:

Delegates generation of identifiers to the database.

  • increment:
    Generates long, short or int identifiers which are only unique if no other process inserts data into the same table.
    Should not be used in a clustered environment.

  • identity:
    Use of the DB2, MySQL, MS SQL Server, Sybase et HypersonicSQL identity column. The returned identifier is long, short or int.

  • sequence:
    Use of DB2, PostgreSQL, Oracle, SAP DB, McKoi or Interbase generator sequences. The returned identifier is long, short or int.

  • hilo:
    Uses a hi/lo algorithm to efficiently generate long, short or int identifiers, using a table and a column (by default, hibernate_unique_key and next_hi respectively) as the source of the “hi” value. The hi/algorithm only generates unique identifiers for a particular database.

  • seqhilo:
    Uses a hi/lo algorithm to efficiently generate long, short or int identifiers, given a sequence name as the basis.

  • uuid:
    Uses a UUID 128 bits type algorithm to generate string identifiers, unique within a network (the IP address is used). Le UUID is encoded in a string of 32 hexadecimal numbers.

  • guid:
    Uses a GUID string generated by the databse for MS SQL Server and MySQL.

  • assigned:
    Lets the application affect an identifier to the object before the save() method is called. This is the default strategy if no <generator> has been specified.

  • select:
    Retrieves a primary key assigned by a trigger by selecting the line by a given unique key.

  • foreign:
    Uses the identifier of an associated object. Generally used in conjunction with a <one-to-one> association on the primary key.

Composite identifiers

We refer to composite identifiers where a persistent class contains more than one identifier. These composite identifiers are mapped on composite primary keys at SQL model level.

The presence of a composite identifier leads to a certain number of constraints: Your persistent classes must overload the equals() and hashCode() methods. They must also implement the Serializable interface.

If these methods are missing, the Hibernate Designer module will automatically create skeletons of these operations during Java generation. It will also add an implementation link to the Serializable interface where necessary.

Hibernate_Fig5_English.gif (9.98 KB) admin admin, 15 December 2015 09:45