Sunday, 29 April 2012

Hibernate Introduction Part 2

Hibernate Annotations
This example is the same as the first example except that it uses annotations. There we first started
by creating the .hbm.xml file, here there is no need to create it instead we will use annotations to do
the object relational mapping.

Step 1 : Remove the patient.hbm.xml file

Step 2 ; Open the hibernate.cfg.xml and replace the following line

<!-- Mapping files -->
<mapping resource="patient.hbm.xml" />


<!-- Mapping Classes -->
<mapping class="mypack.patient" />

Step 3 : Open the and replace as follows

package mypack;

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

public class patient {

private Integer id;
private String firstName;
private String lastName;

public Integer getId() {
return id;

public void setId(Integer id) { = id;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;


Step 4:  No Change in . Just right click as and run as Java application.

No comments:

Post a Comment