Hibernate: Syntax of O/R Mapping File

The root element of an XML mapping document is <hibernate-mapping>, which has many optional attributes. The list of possible attributes with their possible values is shown below:

<hibernate-mapping package=”package.name” auto-import=”true|false”

schema=”schemaName” catalog=”catalogName” default-cascade=”cascade_style”

default-access=”field|property|ClassName” default-lazy=”true|false”/>

A short description of each of these attributes is given below:

package: Specifies a package prefix to be used for unqualified class names

auto-import (default is true): Specifies whether we can use unqualified class names in the query language

schema: The name of a database schema

catalog: The name of a database catalog

default-cascade (default is none): The default cascade style

default-access (default is property): Specifies the strategy Hibernate should use to access all properties. It may be a custom implementation of PropertyAccessor. When default-access is property, Hibernate identifies property names corresponding to table columns from mapping files. It then uses corresponding get and set methods to access the values of the object fields.

default-lazy (default is true): Specifies the policy for unspecified lazy attributes of class and collection mappings

The <class> element

This element maps the domain object with the corresponding entity in the database. The <hibernate- mapping> element allows us to nest several persistent <class> mappings. However, it recommended to map only a single persistent class in one mapping file and name it after the persistent superclass, e.g. Book. hbm.xml, Employee.hbm.xml. The list of possible attributes with their possible values is shown below:

<class

name=”ClassName”

table=”tableName”

discriminator-value=”discriminator_value”

mutable=”true|false”

schema=”owner”

catalog=”catalog”

proxy=”ProxyInterface”

dynamic-update=”true|false”

dynamic-insert=”true|false”

select-before-update=”true|false”

polymorphism=”implicit|explicit”

where=”arbitrary sql where condition”

persister=”PersisterClass”

batch-size=”N”

optimistic-lock=”none|version|dirty|all”

lazy=”true|false”

entity-name=”EntityName”

catalog=”catalog”

check=”arbitrary sql check condition” rowid=”rowid”

subselect=”SQL expression” abstract=”true|false”

/>

The following is a brief description of each of the possible attributes:

name (optional):

The fully qualified Java class name of the persistent class/interface. If this attribute is absent, it is considered that the mapping is for a non-POJO entity.

table (optional – defaults to the unqualified class name):

The name of table in database

discriminator-value (optional – defaults to the class name):

A value that distinguishes individual subclasses, used for polymorphic behaviour

mutable (optional, defaults to true):

Specifies if the instances of the class are mutable

schema (optional):

Override the schema name specified by the root <hibernate-mapping> element

catalog (optional):

Override the catalog name specified by the root <hibernate-mapping> element

proxy (optional):

Specifies an interface to use for lazy initializing proxies

dynamic-update (optional, defaults to false):

Specifies that update SQL statement should be generated at runtime and contain only those columns whose values have changed

dynamic-insert (optional, defaults to false):

Specifies that insert SQL statements should be generated at runtime and contain only the columns whose values are not null

select-before-update (optional, defaults to false):

Specifies that Hibernate should never perform an SQL update unless it is certain that an object is actually modified

polymorphism (optional, defaults to implicit):

Determines whether implicit or explicit query polymorphism is used

where (optional):

Specifies an arbitrary SQL WHERE condition to be used when retrieving objects of this class

persister (optional):

Specifies a custom ClassPersister

batch-size (optional, defaults to 1):

Specifies the batch size for fetching instances of this class by identifier

optimistic-lock (optional, defaults to version):

Determines the optimistic locking strategy

lazy (optional):

Lazy fetching may be completely disabled by setting lazy-‘false”

entity-name (optional):

Hibernate version 3 allows a class to be mapped multiple times and also allows entity mappings that are represented by Maps or XML at the Java level. In these cases, we provide an arbitrary name for the entity explicitly.

catalog (optional):

The name of a database catalog used for this class and its table

check (optional):

An SQL expression used to generate a multi-row check constraint for automatic schema generation

rowid (optional):

Hibernate can use the rowid extra column (for databases such as Oracle) for fast updates if this option is set to rowid

subselect (optional):

Maps an immutable and read-only entity to a database subselect. Useful if you want to have a view instead of a base table, but don’t

abstract (optional):

Used to mark abstract super classes in <union-subclass> hierarchies

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *