JPA Entity Relationship in RDBMS

Posted By : Ankur Bansala | 27-May-2018

 

 

 

 Introduction:

In this blog, we are going to explore the relationship between entities in JPA.

If you want to store a data , now how do you store the data so normally when you say that you are going to store data in a file which is your text file, CSV, or maybe XLS but the problem is if you are storing your data in all this format, it is very difficult to fetch that's why we have this amazing scientist dr. Cod who gives the concept of the relational database.

 Now, let’s think about OOPs concept, can we achieve oops concept on tables. For this, we wanted a mapping tool where you can map your objects and relational properly (keeping in mind of Oops concept) that's why we have a concept of ORM which stands for Object Relational Mapping. We have an ORM tool which is Hibernate and Hibernate is an implementation of JPA.  

 

 

 

 The Types of Relationships between Entity classes are as follows:

                       • @OneToOne Relation

                       • @OneToMany Relation

                       • @ManyToMany Relation

 • @OneToOne Relation

In the following example, you will learn how to map one-to-one relationship Consider the following relationship between Laptop and Student entity.

One student owns only one laptop so which means one to one and one laptop can belong to any one student so there is one to one mapping.

 Let’s take an example:
We are having two Entity classes name Student.java and Laptop.java.
Here one student having one laptop or we can say that one laptop only used by one student So it's called OneToOne mapping.

Student.java 

 

 

 

 

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType
import javax.persistence.Id;
import javax.persistence.OneToOne; 

@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
private int rollNo;
private String name;
private int marks;
@OneToOne
private Laptop laptop;
public int getRollNo(){
	return rollNo
}
public void setRollNo(int rollNo) {
	this.rollNo=rollNo;
}
public String getName() {
	return name;
}
public void setName(String name){
	this.name=name;
}
public int getMarks() { 
        return marks; 
} 
public void setMarks(int marks) { 
        this.marks = marks; 
} 
public Laptop getLaptop() { 
      return laptop; 
} 
public void setLaptop(Laptop laptop) { 
        this.laptop = laptop; 
    } 
     
}

Laptop.java

 

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Laptop {

	@Id
	private int lid;
	private String lname;

	public int getLid() {
		return lid;
	}

	public void setLid(int lid) {
		this.lid = lid;
	}

	public String getLname() {
		return lname;
	}

	public void setLname(String lname) {
		this.lname = lname;
	}

}

 

 

 

 Tables will create in db as fillows: 

 image

 

 

 

Note: Student table having the laptop_lid colum as foreign key and in Laptop table lid colum is the primary key for Laptop table.

 

 

 

@OneToMany Relation

In a simple way, mapping (one-to-many ) state that one row in a table is mapped to multiple rows in another table.

Bidirectional relationship occurs between the objects which means that we can access Object A from Object B and Object B from Object A.

 

 

 

For ex:

For student class -- >

   @Entity
public class Student {

	@Id
	private int rollno;
	private String name;
	private int marks;
	@OneToMany
	private List<Laptop> laptop = new ArrayList<>();

// getter -- setter
}

 

for Laptop class -- >

@Entity
public class Laptop {

	@Id
	private int lid;
	private String lname;
	@ManyToOne
        private Student student;

        //setter - getter
}

 

 

 

Note : JPA will create student , laptop and student_laptop tables as shown below :

image

 

 

 

Now , you can see one more table (student_laptop) is created. If you dont want to let it create , you can simply do some changes in your entity class as described below :

 

@Entity
public class Student {

	@Id
	private int rollno;
	private String name;
	private int marks;
	@OneToMany(mappedBy="student")
	private List<Laptop> laptop = new ArrayList<>();

}

@Entity
public class Laptop {

	@Id
	private int lid;
	private String lname;
	@ManyToOne
	private Student student;
}  

Now, JPA will create only two tables (Laptop and Student) shown below: 

Image 

 

 

 

@ManyToMany Relation

If you have many to many relationships, so one student can use multiple laptops and multiple laptops can belong to one student so multiple laptops can be used by multiple students so we have many to many relationships here.

To achieve this let's create our Entity class first.

 

@Entity
public class Student {

	@Id
	private int rollno;
	private String name;
	private int marks;
	@ManyToMany
	private List<Laptop> laptop = new ArrayList<>();
}

 

@Entity
public class Laptop {

	@Id
	private int lid;
	private String lname;
	@ManyToMany
	private List<Student> student = new ArrayList<>();
}

 Image

 

 

 

Note : we are having one more extra table student_laptop. For get rid from extra table you have to use mappedBy functionality in your entity class as shown below:

 

@Entity
public class Student {

	@Id
	private int rollno;
	private String name;
	private int marks;
	@ManyToMany(mappedBy="student")
	private List<Laptop> laptop = new ArrayList<>();
}


@Entity
public class Laptop {

	@Id
	private int lid;
	private String lname;
	@ManyToMany
	private List<Student> student = new ArrayList<>();
}

Now Schema looks like:

Image 

I hope this will help you to explore further. 

 

 

 

About Author

Author Image
Ankur Bansala

Ankur is an experienced Java back-end Developer and having capabilities to build web application.

Request for Proposal

Name is required

Comment is required

Sending message..