Hibernate Mapping File

More about Hibernate mapping file
  • Each hibernate mapping file must contain only one <id> (or relevant tag <composite-id>)
  • Java object identified uniquely by the <id> tag property.
  • <id> tag property corresponding column can be primary key or non-primary key in the database
  • In mapping file class names and property names are names are not case sensitive.
  • When the property name and column name both are same we no need to give ‘column’ attribute
  • When the Persistent class name and table name both are same we no need to give ‘table’ attribute
  • Generally we write one mapping file per one domain object. But it allows writing multiple objects mapping information within the same mapping file. Per each class mapping we need to write one <class> tag.
  • Databases have different ways to organize its tables. Some database places all tables in a different “schemas”, some database places all tables in a different “catalogs”. If we want we can specify this in <class> tag of the mapping file
  • In mapping file we write fully qualified name of the entity class in “name” attribute of <class> tag. Instead of that we can write package name separately using “package” attribute of <hibernate-mapping> tag
  • In the mapping file we no need to map all the properties of the entity and all the columns of table. As per our requirement we configure required properties of the entity and columns of the table

In our examples if we observe, we used the following annotations

@Id
@Column
@Entity
@Table
@GeneratedValue
@Temporal
  • All the above annotations we are taking from java.persistence package. Actually this package is not the part of hibernate API. This package is from JPA(Java Persistence API).
  • These annotations also given by hibernate. But we don’t prefer to use hibernate given annotations. We prefer to use JPA annotations. Reason for this is, Hibernate is a specific API, where as JPA is a specification.
  • If we use JPA annotations we have to flexibility to change the implementation vendor without changing application code.
  • JPA is an APl(from SUN), its not the implementation. There are multiple implementations are there for JPA. Some of famous implementation of JPA are OpenJPA, Hibernate, Toplink Essentials, Eclipselink… etc.