Mapping the Java Objects

From Documentation

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.

There are two ways to tell Hibernate how to load and store objects of the persistent class, one is using Hibernate mapping file, and the other is using Java Annotation.

Using the Mapping Files

  1. Simply create Event.hbm.xml for the persistent class
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

     <class name="events.Event" table="EVENTS">
         <id name="id" column="EVENT_ID">
             <generator class="native"/>
             <property name="date" type="timestamp" column="EVENT_DATE"/>
             <property name="title"/>
  1. Place this Event.hbm.xml in the directory called src in the development folder, and its correct package. (ex.$myApp/WEB-INF/src/event/Event.hbm.xml)

Using Java Annotation

The benefit of using Java annotation instead of Hibernate mapping file is that no additional file is required. Simply add Java annotation on your Java class to tell Hibernate about the mappings.

 package events;

 import java.util.Date;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Table;

 public class Event {
     private Long id;
     private String title;
     private Date date;

     @Column(name = "EVENT_ID")
     public Long getId() {
         return id;
     private void setId(Long id) { = id;
     @Column(name = "EVENT_DATE")
     public Date getDate() {
         return date;
     public void setDate(Date date) { = date;
     public String getTitle() {
         return title;
     public void setTitle(String title) {
         this.title = title;
  • @Entity declares this class as a persistence object
  • @Table(name = "EVENTS") annotation tells that the entity is mapped with the table EVENTS in the database
  • @Column element is used to map the entities with the column in the database.
  • @Id element defines the mapping from that property to the primary key column.

Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.