Customizing the schema in Hibernate by R4R Team

Many Hibernate mapping elements always define optional attributes named length, precision and scale.  Then we can set the length, precision and scale of a column with this attribute.

<property name="zip" length="5"/>
<property name="balance" precision="12" scale="2"/>

Some tags also accept a not-null attribute for generating a NOT NULL constraint on table columns, and a unique attribute for generating UNIQUE constraint on table columns.

<many-to-one name="bar" column="barId" not-null="true"/>
<element column="serialNumber" type="long" not-null="true" unique="true"/>

A unique-key attribute can be used to group columns in a single, unique key constraint. Currently, the specified value of the unique-key attribute is not used to name the constraint in the generated DDL. It is only used to group the columns in the mapping file.

<many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/>
<property name="employeeId" unique-key="OrgEmployee"/>

An index attribute specifies the name of an index that will be created using the mapped column or columns. Multiple columns can be grouped into the same index by simply specifying the same index name.

<property name="lastName" index="CustName"/>
<property name="firstName" index="CustName"/>

A foreign-key attribute can be used to override the name of any generated foreign key constraint.

<many-to-one name="bar" column="barId" foreign-key="FKFooBar"/>

Many mapping elements also accept a child <column> element. This is particularly useful for mapping multi-column types:

<property name="name" type="my.customtypes.Name"/>
    <column name="last" not-null="true" index="bar_idx" length="30"/>
    <column name="first" not-null="true" index="bar_idx" length="20"/>
    <column name="initial"/>

The default attribute allows you to specify a default value for a column.You should assign the same value to the mapped property before saving a new instance of the mapped class.

<property name="credits" type="integer" insert="false">
    <column name="credits" default="10"/>
<version name="version" type="integer" insert="false">
    <column name="version" default="0"/>

The sql-type attribute allows the user to override the default mapping of a Hibernate type to SQL datatype.

<property name="balance" type="float">
    <column name="balance" sql-type="decimal(13,3)"/>

The check attribute allows you to specify a check constraint.

<property name="foo" type="integer">
    <column name="foo" check="foo > 10"/>
<class name="Foo" table="foos" check="bar < 100.0">
    <property name="bar" type="float"/>

The following table summarizes these optional attributes.






column length



column decimal precision



column decimal scale



specifies that the column should be non-nullable



specifies that the column should have a unique constraint



specifies the name of a (multi-column) index



specifies the name of a multi-column unique constraint



specifies the name of the foreign key constraint generated for an association, for a <one-to-one>, <many-to-one>, <key>, or <many-to-many> mapping element. Note that inverse="true" sides will not be considered by SchemaExport.


SQL column type

overrides the default column type (attribute of <column> element only)


SQL expression

specify a default value for the column


SQL expression

create an SQL check constraint on either column or table

The <comment> element allows you to specify comments for the generated schema.

<class name="CustomerName" table="CurCust">
    <comment>Current customers only</comment>

<property name="balance">
    <column name="bal">
        <comment>Balance in USD</comment>

We can understand with the results in a comment on table or comment on column statement in the generated DDL where supported.
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!