For object relational mapping we have to write a mapping file for the simple Java class.
Following example illustrates this mapping.
public class Employee
{
private long empId=0;
private String Name="";
private int salary=0;
{
return salary;
}
{
this.salary=salary;
}
public string getName()
{
return Name;
}
public void setName(string Name)
{
this.Name=Name;
}
public long getEmpId()
{
return empId;
}
public void setEmpId(long empId)
{
this.empId=empId;
}
}//end of class Employee
Now a simple mapping file can be written. Writing a mapping file is the most important thing in hibernate.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Employee" table="EmpTable" >
<id name="empId" type="java.lang.Long" column="emp_id" >
<generator class="increment" />
</id>
<property name="Name" type="java.lang.String" column="Empname" length="20" />
<property name="salary" type="java.lang.Integer" column="Salary" length="5" />
</class>
</hibernate-mapping>
In above mapping file we are mapping the class Employee to the database table EmpTable. Note that the generator class is increment because this type of class generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table.
The object or relational mapping are usually defined in an XML document and readable and editable files.
0 comments:
Post a Comment