What is composite primary key in hibernate with example

In hibernate each POJO class represents a table and it's instance represents a row in the table. It is very common to have tables where two or more columns make the primary key. In such cases the composite primary key is represented with another class which has a composition relationship with the class that represents the table.

<composite-id
name="propertyName"
class="ClassName"
unsaved-value="undefined|any|none"
access="field|property|ClassName">
<key-property name="propertyName" type="typename" column="column_name"/>
<key-many-to-one name="propertyName class="ClassName" column="column_name"/>
</composite-id>

  • name (optional): A property of component type that holds the composite identifier.
  • class (optional - defaults to the property type determined by reflection): The component class used as a composite identifier.
  • unsaved-value (optional - defaults to undefined): Indicates that transient instances should be considered newly instantiated, if set to any, or detached, if set to none. It is best to leave the default value in all cases.

Search:

Recent Posts