Hibernate Annotations

Published: Sunday, 13 November 2005
Last modified: Monday, 25 October 2010

Java 1.5 required

Annotations are supported in Java 1.5 and later. If you are using 1.4 you can use XDoclet to help generate your mapping files.

Basic column mapping

unique column restraint

To specifiy an unique key constraint on a column use

@Column(unique=true)

Enumerations

Enums are saved as either text or ordinals depending on the underlying column.

Ignored or transient fields

To prevent persistence of a field, annotate it with @Transient so it will not be mapped by hibernate.

Cascade all-delete-orphan

The standard EJB annotations do not have the delete orphan option.

@OneToMany {cascade=CascadeType.ALL}

The above is from the javax.persistence package.

Instead, we can use the original Hibernate mapping attributes this way:

@org.hibernate.annotations.Cascade(
   {org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN}
)

Links