Hibernate Relational Mapping

August 14, 2018 | Author: Coder's Yard | Category: Data Model, Data Management, Software, Computer Programming, Scientific Modeling
Share Embed Donate


Short Description

Download Hibernate Relational Mapping...

Description

Hibernate Relational Mapping Well the main purpose of hibernate is to integrate into number of databases seamlessly. So in some cases where a bit hectic entity called “Foreign Key/Relation”  comes within a question arises how to tell hibernate of that. Hibernate gives us four main ways of expressing the relationship of one table with other, they are one-toone, one-to-many and many-to-one and many-to-many. So how we map the relation to other table… One-to-one: Exactly a unique row of a master table is to be associated with a unique row of child table or vice versa. One-to-many: Exactly a unique row of a table is to be associated with more than one rows of associate table. Many-to-one: Many rows of a table are associated with exactly one row of associate associate table (Inverse of One-to-many). One-to-many). Many-to-many: One (entire/partial) table is associated with other and vice versa. Well let us take a very simple example of t wo relational based tables with one-to one mapping where a row of an associate table will be dependent on primary key of a master table… Table: Person Field Person_id Fullname

Type String (Primary Key) String

Table: Employee Field Person_id Designation

Type String (Primary Key) String

In our case lets consider Person_id of Employee of  Employee table be “foreign key” that refers to Person_id of Person of Person table. The POJO classes of these tables in normal be as following… File Name: Person.java Package com.pojo; import java.io.Serializable; java.io.Serializable; public class Person implements Serializable { private String person_id; private String fullname;  //Getter and setter method method of Employee persistence persistence object (Employee.java)

private Employee employee; public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee=employee; } public String getPerson_id() { return this.person_id; } public String getFullname() { return this.fullname; } public void setPerson_id(String person_id) { this.person=person; } public void setFullname(String fullname) { this.fullname=fullname; } } File Name: Employee.java Package com.pojo; import java.io.Serializable; public class Employee implements Serializable { private String person_id; private String designation;  //Getter and setter method of Person persistence object (Person.java) private Person person; public Person getPerson() { return this.person;

} Public void setPerson(Person person) { this.person=person; } public String getPerson_id() { return this.person_id; } public String getDesignation() { return this.designation; } public void setPerson_id(String person_id) { this.person=person; } public void setDesignation(String designation) { this. designation = designation; } } The hibernate xml files will be… File Name: person.hbm.xml

File Name: employee.hbm.xml In your action servlet or DAO … For saving import com.pojo.Person; import com.pojo.Employee; …………………………….. …………………………………… String id=”abc”; String name=”XYZ”  String design=”Programmer”;  //Create persistence objects Person personPersistance=new Person(); Employee employeePersistance=new Employee(); employeePersistance.setPerson_id(id); employeePersistance.setDesignation(desig); personPersistance.setPerson_id(id); personPersistance.setFullname(name); personPersistance.setEmployee(employee); personPersistance.save(person);

 //Similarly for update personPersistance.update(person); …………………………….. ……………………………………

For deleting …………………………….. ……………………………………  //Create persistence objects Person personPersistance=new Person(); Employee employeePersistance=new Employee(); employeePersistance.setPerson_id(id); employeePersistance.setDesignation(desig); personPersistance.setPerson_id(id); personPersistance.setFullname(name); personPersistance.delete(person);  //No need to set employeePersistance for personPersistance. …………………………….. ……………………………………

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF