Start New Discussion within our Software Development Community

in this xml mapping file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false"
      mutable="true" name="travel.Person" optimistic-lock="version"
      polymorphism="implicit" select-before-update="false" table="PERSON">
        <id column="PERSONID" name="personId">
            <generator class="increment"/>
        </id>
        <property column="NAME" name="name"/>
        <property column="JOBTITLE" name="jobTitle"/>
        <property column="FREQUENTFLYER" name="frequentFlyer"/>
        <set cascade="all-delete-orphan" inverse="true" lazy="true" name="trips" table="TRIP">
            <key column="PERSONID"/>
            <one-to-many class="travel.Trip"/>
        </set>
    </class>
</hibernate-mapping>

what does this code do?

<id column="PERSONID" name="personId">
            <generator class="increment"/>
        </id>

<id/> tag will define colum "PERSONID" as the primary key for the object Person. It is not required in hibenate that PERSONID colum is a primary key to be declared as a primary key for the object.
Generator = increment means hibernate will treat this column like an AUTO GENERATED or SEQUENCE. When you save a Person object you dont have to pass a value for PERSONID. Hibernate will generate i for you.

This article has been dead for over six months. Start a new discussion instead.