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" />

With


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


Step 3 : Open the patient.java 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;



@Entity
@Table(name="patient")
public class patient {

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

@Id
@GeneratedValue
@Column(name="ID")
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name="firstname")
public String getFirstName() {
return firstName;
}

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

@Column(name="lastname")
public String getLastName() {
return lastName;
}

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

}

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

No comments:

Post a Comment