Search This Blog

Monday, 12 September 2011

Id Generators - 5

I shall cover the select and assigned generators in this article.


This is used when the primary key is to be generated by some database logic. For e.g. the primary key is assigned by a database trigger. As hibernate did not assign the primary key (and it is not a generated value like auto-increment) how does it set the object's id ?

Hibernate needs a unique key so as to  select the row and retrieving the primary key value.
This is illustrated by the Library_Card table
create table LIBRARY_CARD (
        ID varchar(255) not null default 'UNKNOWN',
        PAN_NUMBER varchar(255) not null,
        ISSUE_DATE datetime,
        RENEWAL_DATE datetime,
        primary key (ID),
        unique (PAN_NUMBER)
Each card is associated with a pan number that is unique. The Library Card Id is generated based on the Pan Number provided. The java class for the same is as below:
public class LibraryCard {
    private String panNumber;
    private Date issueDate;
    private Date renewalDate;    
    private String id;
The mapping document is as below:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.model">
    <class name="LibraryCard" table="LIBRARY_CARD">
        <id name="id" type="string">
            <column name="ID" default="UNKNOWN">
            <generator class="select">
                <param name="key">panNumber</param>
                <property name="panNumber" type="string">
                   <column name="PAN_NUMBER"  />
        <property name="issueDate" type="timestamp">
            <column name="ISSUE_DATE"  />
        <property name="renewalDate" type="timestamp">
            <column name="RENEWAL_DATE"  />
The natural id is very important and must be specified.After insert, Hibernate selects the just inserted row to determine the identifier value assigned by the database. The correct row is located using a unique key. This key is identified by the natural-id.
 If the natural-id is not specified then the key param must be used to indicate the unique key that can be used by Hibernate in its select query.
2204 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - saving [c
2204 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - executing
2219 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - executing
 identity-insert immediately
2219 [main] DEBUG org.hibernate.persister.entity.AbstractEntityPersister  - Inse
rting entity: com.model.LibraryCard (native id)
2219 [main] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to open PreparedSt
atement (open PreparedStatements: 0, globally: 0)
2219 [main] DEBUG org.hibernate.SQL  - 
        (?, ?, ?)
2266 [main] DEBUG org.hibernate.jdbc.AbstractBatcher  - about to open PreparedSt
atement (open PreparedStatements: 0, globally: 0)
2266 [main] DEBUG org.hibernate.SQL  - 
        PAN_NUMBER =?
2282 [main] DEBUG org.hibernate.jdbc.AbstractBatcher  - preparing statement
2282 [main] DEBUG org.hibernate.type.StringType  - binding '12AKH58' to paramete
r: 1
2282 [main] DEBUG org.hibernate.type.StringType  - returning '#12AKH58' as colum
n: ID
As can be seen, Hibernate did not attempt to set the id as a part of the object creation process. It let the database generate the value, after which it fired a select query to retrieve the newly created id. The database trigger used here for id creation is as below
create trigger generate_lib_card_PK before insert on library_card for each row
set := CONCAT('#', new.pan_number);


This generator is used if you want the application to assign identifiers, It uses the identifier value already assigned to the object's id property. This is the default behaviour if you do not specify a <generator> element.
Consider the simple table :
create table SLOGAN_DETAILS  (
  ID int(11) not null,
  SLOGAN varchar(100) not null,
alter table SLOGAN_DETAILS add constraint SLOGAN_DETAILS_PK primary key (ID);

The java class and the mapping document are as below:
public class SloganDetails {
    private Long id;
    private BigDecimal population;
    private String slogan;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.model">
    <class name="SloganDetails" table="SLOGAN_DETAILS">    
        <id name="id" column="id">
            <generator class="assigned"/> 

        <property name="population" type="big_decimal">
        <column name="POPULATION" precision="10" />
    <property name="slogan" type="string">
        <column name="SLOGAN" />

The code requires that the id field is set with appropriate value before record is saved.
Session session = sessionFactory.openSession();
SloganDetails details = new SloganDetails();
details.setPopulation(new BigDecimal("1233"));
details.setSlogan("Garden city");
Transaction transaction = session.beginTransaction();;

The logs generated during the save are as below:
2172 [main] DEBUG org.hibernate.jdbc.JDBCContext  - after transaction begin
2172 [main] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener  - sa
ving transient instance
2172 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener  - generated
 identifier: 1, using strategy:
2219 [main] DEBUG org.hibernate.SQL  - 
        (POPULATION, SLOGAN, id) 
        (?, ?, ?)


  1. Hi

    If I am using Hibernate annotations, how to use Generators so that trigger will be used to generate primary key?

    Any help is highly appreciable.

  2. The JPA Annotations that you use do not support the select option. Unfortunately I could not find any hibernate extension for the same. One option is to define your own custom generator (
    Ill continue to hunt for some solution. Will update you once I have one.