Here is my Hibernate/JPA bidirectional mapping short note.
One To One
Student |
StudentRegistration |
@Id
@Column(name="student_id")
private Long studentId; |
@Id
@Column(name="registration_no")
private Long registrationNo; |
@Column(name="student_name")
private String name; |
@Column(name="level")
private String level; |
|
@Column(name="registration_date")
@Temporal(TemporalType.DATE)
private Date regDate; |
|
|
@OneToOne
StudentRegistration studentRegistration; |
@OneToOne(mappedBy="studentRegistration")
Student student; |
Student is the owner, We add owner side property name to 'mappedBy' of other side.
Database tables created by Hibernate
student
student_id |
student_name |
STUDENTRGISTRATION_registration_no |
|
|
|
student_registration
registration_no |
level |
registration_date |
|
|
|
If you want to change
STUDENTRGISTRATION_registration_no column name to
stu_reg_no add @JoinColumn annotation.
@OneToOne
@JoinColumn(name="stu_reg_no")
StudentRegistration studentRegistration;
View Sample Code
Student.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "student")
public class Student implements Serializable {
@Id
@Column(name = "student_id")
private Integer studentId;
@Column(name = "student_name")
private String name;
@OneToOne
//@JoinColumn(name="stu_reg_no")
private StudentRegistration studentRegistration;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public StudentRegistration getStudentRegistration() {
return studentRegistration;
}
public void setStudentRegistration(StudentRegistration studentRegistration) {
this.studentRegistration = studentRegistration;
}
}
StudentRegistration.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name="student_registration")
public class StudentRegistration implements Serializable {
@Id
@Column(name = "registration_no")
private Integer registrationNo;
@Column(name = "level")
private String level;
@Column(name = "registration_date")
@Temporal(TemporalType.DATE)
private Date registrationDate;
@OneToOne(mappedBy="studentRegistration")
private Student student;
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Date getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}
public Integer getRegistrationNo() {
return registrationNo;
}
public void setRegistrationNo(Integer registrationNo) {
this.registrationNo = registrationNo;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
One to Many/ Many to One
Employee |
Department |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="dep_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="dep_name")
private String name; |
|
|
|
|
@ManyToOne
Department department; |
@OneToMany(mappedBy="department")
List<Employee> emps; |
Database tables created by Hibernate
employee
emp_id |
emp_name |
DEPARTMENT_dep_id |
|
|
|
department
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@ManyToOne
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Department.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
@Entity
@Table(name="department")
public class Department implements Serializable {
@Id
@Column(name = "dep_id")
private Long id;
@Column(name = "dep_name")
private String name;
@OneToMany(mappedBy = "department")
List<Employee> emps;
public List<Employee> getEmps() {
return emps;
}
public void setEmps(List<Employee> emps) {
this.emps = emps;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Many to Many
Employee |
Project |
@Id
@Column(name="emp_id")
private Long id; |
@Id
@Column(name="project_id")
private Long id; |
@Column(name="emp_name")
private String name; |
@Column(name="project_name")
private String name; |
|
|
|
|
@ManyToMany(mappedBy="employees")
List<Project> projects; |
@ManyToMany
List<Employee> employees; |
Database tables created by Hibernate
employee
project
project_employee
projects_project_id |
employees_emp_id |
|
|
If you want to change
project_employee table name to
pro_emp add @JoinTable annotaion
@ManyToMany
@JoinTable(name="pro_emp")
List<Employee> employees;
View Sample Code
Employee.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private Long id;
@Column(name = "emp_name")
private String name;
@ManyToMany(mappedBy = "employees")
private List<Project> projects;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Project> getProjects() {
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
}
Project.java
package com.sameera.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
@Entity
@Table(name = "project")
public class Project implements Serializable {
@Id
@Column(name = "project_id")
private Long id;
@Column(name = "project_name")
private String name;
@ManyToMany
List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ReplyDeleteHai Author, Very Good informative blog post,
Thanks for Sharing
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city
blockchain Training in electronic city
Hibernate Training in electronic city
Thanks for sharing article
ReplyDeletearchitect in agra