From 00028b8b19ed41f7d7afc473e8138e1c97ef26ce Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Fri, 21 Apr 2023 04:48:52 +0530 Subject: [PATCH] JPA/Hibernate Associations --- .../baeldung/association/Bidirectional.java | 40 ++++++++ .../baeldung/association/Unidirectional.java | 92 ++++++++++++++++++ .../association/BidirectionalUnitTest.java | 48 +++++++++ .../association/UnidirectionalUnitTest.java | 97 +++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java create mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java create mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java new file mode 100644 index 0000000000..aff5feaeba --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java @@ -0,0 +1,40 @@ +package com.baeldung.association; + +import java.util.List; +import javax.persistence.*; + +@Entity +public class Department { + + @OneToMany(mappedBy = "department") + private List employees; + +} + +@Entity +public class Employee { + + @ManyToOne + @JoinColumn(name = "department_id") + private Department department; + +} + +@Entity +public class Student { + + @ManyToMany(mappedBy = "students") + private List courses; + +} + +@Entity +public class Course { + + @ManyToMany + @JoinTable(name = "course_student", + joinColumns = @JoinColumn(name = "course_id"), + inverseJoinColumns = @JoinColumn(name = "student_id")) + private List students; + +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java new file mode 100644 index 0000000000..3e60b8bdd3 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java @@ -0,0 +1,92 @@ +package com.baeldung.association; + +import javax.persistence.*; + +import java.util.List; +import java.util.Set; + +@Entity +public class Department { + + @Id + private Long id; + + @OneToMany + @JoinColumn(name = "department_id") + private List employees; + +} + +@Entity +public class Employee { + + @Id + private Long id; + + @OneToOne + @JoinColumn(name = "parking_spot_id") + private ParkingSpot parkingSpot; + +} + +@Entity +public class ParkingSpot { + + @Id + private Long id; + +} + +@Entity +public class Student { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + @ManyToOne + @JoinColumn(name = "course_id") + private Course course; + +} + +@Entity +public class Course { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + +} + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + + @ManyToMany + @JoinTable(name = "book_author", + joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + +} + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + +} diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java new file mode 100644 index 0000000000..99d33af12d --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.association; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class BidirectionalUnitTest { + + @Test + public void givenDepartmentWithEmployees_whenGetEmployees_thenReturnListWithEmployees() { + // given + Department department = new Department(); + Employee employee1 = new Employee(); + Employee employee2 = new Employee(); + department.getEmployees().add(employee1); + department.getEmployees().add(employee2); + + // when + List result = department.getEmployees(); + + // then + assertAll("department employees", + () -> assertEquals(2, result.size()), + () -> assertTrue(result.contains(employee1)), + () -> assertTrue(result.contains(employee2)) + ); + } + + @Test + public void givenCourseWithStudents_whenGetStudents_thenReturnListWithStudents() { + // given + Course course = new Course(); + Student student1 = new Student(); + Student student2 = new Student(); + course.getStudents().add(student1); + course.getStudents().add(student2); + + // when + List result = course.getStudents(); + + // then + assertAll("course students", + () -> assertEquals(2, result.size()), + () -> assertTrue(result.contains(student1)), + () -> assertTrue(result.contains(student2)) + ); + } + +} diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java new file mode 100644 index 0000000000..880832c163 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java @@ -0,0 +1,97 @@ +package com.baeldung.association; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@DataJpaTest +public class UnidirectionalUnitTest { + + private final TestEntityManager entityManager; + + public UnidirectionalUnitTest(TestEntityManager entityManager) { + this.entityManager = entityManager; + } + + @Test + public void givenDepartmentWithEmployees_whenFindById_thenDepartmentWithEmployeesReturned() { + // given + Employee employee1 = new Employee(); + Employee employee2 = new Employee(); + Department department = new Department(); + department.setEmployees(List.of(employee1, employee2)); + entityManager.persist(department); + entityManager.flush(); + + // when + Department foundDepartment = entityManager.find(Department.class, department.getId()); + + // then + assertThat(foundDepartment).isEqualTo(department); + assertThat(foundDepartment.getEmployees()).containsExactly(employee1, employee2); + } + + @Test + public void givenStudentWithCourse_whenFindById_thenStudentWithCourseReturned() { + // given + Course course = new Course(); + entityManager.persist(course); + entityManager.flush(); + Student student = new Student(); + student.setCourse(course); + entityManager.persist(student); + entityManager.flush(); + + // when + Student foundStudent = entityManager.find(Student.class, student.getId()); + + // then + assertThat(foundStudent).isEqualTo(student); + assertThat(foundStudent.getCourse()).isEqualTo(course); + } + + @Test + public void givenEmployeeWithParkingSpot_whenFindById_thenEmployeeWithParkingSpotReturned() { + // given + ParkingSpot parkingSpot = new ParkingSpot(); + entityManager.persist(parkingSpot); + entityManager.flush(); + Employee employee = new Employee(); + employee.setParkingSpot(parkingSpot); + entityManager.persist(employee); + entityManager.flush(); + + // when + Employee foundEmployee = entityManager.find(Employee.class, employee.getId()); + + // then + assertThat(foundEmployee).isEqualTo(employee); + assertThat(foundEmployee.getParkingSpot()).isEqualTo(parkingSpot); + } + + @Test + public void givenBookWithAuthors_whenFindById_thenBookWithAuthorsReturned() { + // given + Author author1 = new Author(); + Author author2 = new Author(); + entityManager.persist(author1); + entityManager.persist(author2); + entityManager.flush(); + Book book = new Book(); + book.setAuthors(Set.of(author1, author2)); + entityManager.persist(book); + entityManager.flush(); + + // when + Book foundBook = entityManager.find(Book.class, book.getId()); + + // then + assertThat(foundBook).isEqualTo(book); + assertThat(foundBook.getAuthors()).containsExactly(author1, author2); + } + +}