Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In this tutorial, we will demonstrate a simple one-to-many relationship example in Java using a school with students. A school can have multiple students, but each student belongs to only one school. We will model this relationship using two classes: School
and Student
.
Student
class:public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Student{name='" + name + "', age=" + age + '}'; } }
School
class:import java.util.ArrayList; import java.util.List; public class School { private String name; private List<Student> students; public School(String name) { this.name = name; this.students = new ArrayList<>(); } public String getName() { return name; } public void addStudent(Student student) { this.students.add(student); } public List<Student> getStudents() { return students; } @Override public String toString() { return "School{name='" + name + "', students=" + students + '}'; } }
public class Main { public static void main(String[] args) { School school = new School("ABC School"); Student student1 = new Student("John Doe", 15); Student student2 = new Student("Jane Smith", 16); Student student3 = new Student("Michael Brown", 17); school.addStudent(student1); school.addStudent(student2); school.addStudent(student3); System.out.println(school); } }
Output:
School{name='ABC School', students=[Student{name='John Doe', age=15}, Student{name='Jane Smith', age=16}, Student{name='Michael Brown', age=17}]}
In this example, we created a School
class and a Student
class to model a one-to-many relationship between a school and its students. A school can have multiple students, but each student belongs to only one school. This relationship is demonstrated by the students
list in the School
class, which stores references to the Student
objects.
Hibernate One-to-Many Mapping in Java:
@OneToMany
and @ManyToOne
.@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods } @Entity public class Child { @ManyToOne @JoinColumn(name = "parent_id") private Parent parent; // other fields and methods }
Spring Data JPA One-to-Many Example:
@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods } @Entity public class Child { @ManyToOne @JoinColumn(name = "parent_id") private Parent parent; // other fields and methods }
Java Hibernate Bidirectional One-to-Many Mapping:
@OneToMany
and @ManyToOne
annotations in the related entities.@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods } @Entity public class Child { @ManyToOne @JoinColumn(name = "parent_id") private Parent parent; // other fields and methods }
Java JPA Unidirectional One-to-Many Relationship:
@Entity public class Parent { @OneToMany @JoinColumn(name = "parent_id") private List<Child> children; // other fields and methods }
Java Hibernate One-to-Many Annotation:
@OneToMany
annotation is used to define a one-to-many relationship in Hibernate.@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods }
OneToMany Mapping in Java Using Annotations:
@OneToMany
and @JoinColumn
are used to map one-to-many relationships.@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods }
Java Hibernate mappedBy
Attribute Explanation:
mappedBy
attribute in @OneToMany
indicates the field in the child class that owns the relationship.@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods }
Java JPA CascadeType
in One-to-Many Relationships:
CascadeType
defines how operations (like persist, remove) should cascade from parent to child entities.@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List<Child> children;
Java Hibernate orphanRemoval
in One-to-Many:
orphanRemoval
attribute removes child entities when they are removed from the collection.@OneToMany(mappedBy = "parent", orphanRemoval = true) private List<Child> children;
Spring Boot JPA One-to-Many Relationship:
@Entity public class Parent { @OneToMany(mappedBy = "parent") private List<Child> children; // other fields and methods }
Java JPA FetchType
in One-to-Many Mapping:
FetchType
defines how the associated entities are loaded. LAZY
loads them when needed, while EAGER
loads them immediately.@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) private List<Child> children;
OneToMany Mapping with JoinTable
in Java:
@JoinTable
annotation is used to map a many-to-many relationship using an intermediate table.@Entity public class Parent { @OneToMany @JoinTable(name = "parent_child", joinColumns = @JoinColumn(name = "parent_id"), inverseJoinColumns = @JoinColumn(name = "child_id")) private List<Child> children; // other fields and methods }
Java Hibernate Lazy Loading in One-to-Many:
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) private List<Child> children;