From 00028b8b19ed41f7d7afc473e8138e1c97ef26ce Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Fri, 21 Apr 2023 04:48:52 +0530 Subject: [PATCH 1/5] 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); + } + +} From 82f9d6a50060568bbe679c12223324b074fa5427 Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Thu, 27 Apr 2023 05:00:38 +0530 Subject: [PATCH 2/5] Hibernate Associations --- .../main/java/com/baeldung/HibernateUtil.java | 2 + .../baeldung/association/Bidirectional.java | 40 ------ .../baeldung/association/Unidirectional.java | 92 ------------- .../associations/biredirectional/Course.java | 24 ++++ .../biredirectional/Department.java | 21 +++ .../biredirectional/Employee.java | 19 +++ .../associations/biredirectional/Student.java | 20 +++ .../associations/unidirectional/Author.java | 17 +++ .../associations/unidirectional/Book.java | 20 +++ .../unidirectional/Department.java | 33 +++++ .../associations/unidirectional/Employee.java | 42 ++++++ .../unidirectional/ParkingSpot.java | 11 ++ .../association/BidirectionalUnitTest.java | 48 ------- .../association/UnidirectionalUnitTest.java | 97 ------------- .../associations/BidirectionalUnitTest.java | 103 ++++++++++++++ .../hibernate/associations/DataJpaTest.java | 5 + .../associations/UnidirectionalUnitTest.java | 128 ++++++++++++++++++ 17 files changed, 445 insertions(+), 277 deletions(-) delete mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java delete mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Department.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Department.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Employee.java create mode 100644 persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/ParkingSpot.java delete mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java delete mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java create mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java create mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java create mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java index 26ad7e77ba..8af6b12bae 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java @@ -7,6 +7,7 @@ import org.hibernate.service.ServiceRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +// import com.baeldung.associations.unidirectional.Department; import com.baeldung.manytomany.model.Employee; import com.baeldung.manytomany.model.Project; import com.baeldung.uuids.WebSiteUser; @@ -29,6 +30,7 @@ public class HibernateUtil { configuration.addAnnotatedClass(Element.class); configuration.addAnnotatedClass(Reservation.class); configuration.addAnnotatedClass(Sale.class); + // configuration.addAnnotatedClass(Department.class); configuration.configure("manytomany.cfg.xml"); LOGGER.debug("Hibernate Annotation Configuration loaded"); 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 deleted file mode 100644 index aff5feaeba..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Bidirectional.java +++ /dev/null @@ -1,40 +0,0 @@ -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 deleted file mode 100644 index 3e60b8bdd3..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/association/Unidirectional.java +++ /dev/null @@ -1,92 +0,0 @@ -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/main/java/com/baeldung/associations/biredirectional/Course.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java new file mode 100644 index 0000000000..aadc08c090 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java @@ -0,0 +1,24 @@ +package com.baeldung.associations.biredirectional; +import jakarta.persistence.*; +import java.util.List; + +@Entity +public class Course { + + @Id + private Long id; + + private String name; + + @ManyToMany + @JoinTable(name = "course_student", + joinColumns = @JoinColumn(name = "course_id"), + inverseJoinColumns = @JoinColumn(name = "student_id")) + private List students; + + public List getStudents() { + return students; + } + + //getters and setters +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Department.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Department.java new file mode 100644 index 0000000000..23f56ccd47 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Department.java @@ -0,0 +1,21 @@ +package com.baeldung.associations.biredirectional; + +import java.util.List; +import jakarta.persistence.*; + + +@Entity +public class Department { + + @Id + private Long id; + + @OneToMany(mappedBy = "department") + private List employees; + + public List getEmployees() { + return employees; + } + + +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java new file mode 100644 index 0000000000..92514b9b8c --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java @@ -0,0 +1,19 @@ +package com.baeldung.associations.biredirectional; + +import jakarta.persistence.*; + + +@Entity +public class Employee { + + @Id + private Long id; + + private String name; + + @ManyToOne + @JoinColumn(name = "department_id") + private Department department; + + //getters and setters +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java new file mode 100644 index 0000000000..a494f2475f --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java @@ -0,0 +1,20 @@ +package com.baeldung.associations.biredirectional; + +import java.util.List; + +import jakarta.persistence.*; + +@Entity +public class Student { + + @Id + private Long id; + + private String name; + + @ManyToMany(mappedBy = "students") + private List courses; + + // getters and setters +} + diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java new file mode 100644 index 0000000000..6a106071a1 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java @@ -0,0 +1,17 @@ +package com.baeldung.associations.unidirectional; +import jakarta.persistence.*; +import java.util.Set; + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + @ManyToMany(fetch = FetchType.LAZY, mappedBy = "authors") + private Set books; + +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java new file mode 100644 index 0000000000..1467a819a0 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java @@ -0,0 +1,20 @@ +package com.baeldung.associations.unidirectional; +import jakarta.persistence.*; +import java.util.Set; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable(name = "book_author", + joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors; + +} diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Department.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Department.java new file mode 100644 index 0000000000..3d65f2e3ef --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Department.java @@ -0,0 +1,33 @@ +package com.baeldung.associations.unidirectional; + +import jakarta.persistence.*; +import java.util.List; + +@Entity +public class Department { + + @Id + private Long id; + + @OneToMany(fetch = FetchType.LAZY) + @JoinColumn(name = "department_id") + private List employees; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } +} + + diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Employee.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Employee.java new file mode 100644 index 0000000000..c9fa2c7483 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Employee.java @@ -0,0 +1,42 @@ +package com.baeldung.associations.unidirectional; +import jakarta.persistence.*; + +@Entity +public class Employee { + + @Id + private Long id; + + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parking_spot_id") + private ParkingSpot parkingSpot; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "department_id") + private Department department; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public ParkingSpot getParkingSpot() { + return parkingSpot; + } + + public void setParkingSpot(ParkingSpot parkingSpot) { + this.parkingSpot = parkingSpot; + } + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } +} + diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/ParkingSpot.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/ParkingSpot.java new file mode 100644 index 0000000000..6495d895eb --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/ParkingSpot.java @@ -0,0 +1,11 @@ +package com.baeldung.associations.unidirectional; + +import jakarta.persistence.*; + +@Entity +public class ParkingSpot { + + @Id + private Long id; + +} 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 deleted file mode 100644 index 99d33af12d..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/BidirectionalUnitTest.java +++ /dev/null @@ -1,48 +0,0 @@ -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 deleted file mode 100644 index 880832c163..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/association/UnidirectionalUnitTest.java +++ /dev/null @@ -1,97 +0,0 @@ -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); - } - -} diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java new file mode 100644 index 0000000000..5b7e01bff1 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java @@ -0,0 +1,103 @@ +// package com.baeldung.hibernate.associations; + +// import org.junit.jupiter.api.Test; +// import org.junit.jupiter.api.extension.ExtendWith; +// import org.springframework.beans.factory.annotation.Autowired; +// import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +// import org.springframework.test.context.junit.jupiter.SpringExtension; + +// import java.util.Arrays; +// import java.util.List; +// import com.baeldung.associations.biredirectional.*; + + +// import static org.assertj.core.api.Assertions.assertThat; + +// @ExtendWith(SpringExtension.class) +// @DataJpaTest +// public class BidirectionalHibernateIntegrationTest { + +// @Autowired +// private Course courseRepository; + +// @Autowired +// private Student studentRepository; + +// @Test +// public void whenAddingStudentsToCourse_thenCourseHasStudents() { +// // given +// Student student1 = new Student(); +// student1.setName("John"); +// Student student2 = new Student(); +// student2.setName("Jane"); +// studentRepository.saveAll(Arrays.asList(student1, student2)); + +// Course course = new Course(); +// course.setName("History"); +// courseRepository.save(course); + +// // when +// List students = studentRepository.findAll(); +// course.setStudents(students); +// courseRepository.save(course); + +// // then +// Course result = courseRepository.findById(course.getId()).get(); +// assertThat(result.getStudents()).containsExactlyInAnyOrder(student1, student2); +// } + +// } + +package com.baeldung.hibernate.associations; + + +import com.baeldung.associations.biredirectional.*; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; + +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/associations/DataJpaTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java new file mode 100644 index 0000000000..184fae20c0 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java @@ -0,0 +1,5 @@ +package com.baeldung.hibernate.associations; + +public @interface DataJpaTest { + +} diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java new file mode 100644 index 0000000000..790110a6d7 --- /dev/null +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java @@ -0,0 +1,128 @@ + +// // import org.junit.jupiter.api.Assertions; +// // import org.junit.jupiter.api.Test; +// // import org.springframework.beans.factory.annotation.Autowired; +// // import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +// // import org.springframework.boot.test.context.SpringBootTest; +// // import org.springframework.transaction.annotation.Transactional; + +// // import java.util.Collections; +// // import java.util.List; + +// // @DataJpaTest +// // @Transactional +// // public class UnidirectionalUnitTest { + +// // @Autowired +// // private EntityManager entityManager; + +// // @Test +// // public void givenBookWithAuthor_whenSaved_thenFindBookByAuthor() { +// // // given +// // Author author = new Author(); +// // author.setName("John Doe"); + +// // Book book = new Book(); +// // book.setTitle("My Book"); +// // book.setAuthors(Collections.singleton(author)); + +// // entityManager.persist(author); +// // entityManager.persist(book); + +// // entityManager.flush(); +// // entityManager.clear(); + +// // // when +// // List booksByAuthor = entityManager.createQuery( +// // "select b from Book b join b.authors a where a.name = :name", Book.class) +// // .setParameter("name", "John Doe") +// // .getResultList(); + +// // // then +// // Assertions.assertEquals(1, booksByAuthor.size()); +// // Assertions.assertEquals(book, booksByAuthor.get(0)); +// // } +// // } + + +// package com.baeldung.hibernate.associations; + +// import com.baeldung.associations.unidirectional.*; + +// import org.junit.jupiter.api.Test; +// import org.springframework.beans.factory.annotation.Autowired; +// import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +// import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +// import java.util.List; +// import java.util.Set; + +// import static org.assertj.core.api.Assertions.assertThat; + +// @DataJpaTest +// public class UnidirectionalUnitTest { + +// @Autowired +// private TestEntityManager 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 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); +// } + +// } + + From 1767ea3cdc4982644331c213abefc751d64bc20f Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Thu, 27 Apr 2023 05:10:50 +0530 Subject: [PATCH 3/5] Hibernate Associations --- .../associations/BidirectionalUnitTest.java | 103 -------------- .../hibernate/associations/DataJpaTest.java | 5 - .../associations/UnidirectionalUnitTest.java | 128 ------------------ 3 files changed, 236 deletions(-) delete mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java delete mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java delete mode 100644 persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java deleted file mode 100644 index 5b7e01bff1..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/BidirectionalUnitTest.java +++ /dev/null @@ -1,103 +0,0 @@ -// package com.baeldung.hibernate.associations; - -// import org.junit.jupiter.api.Test; -// import org.junit.jupiter.api.extension.ExtendWith; -// import org.springframework.beans.factory.annotation.Autowired; -// import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -// import org.springframework.test.context.junit.jupiter.SpringExtension; - -// import java.util.Arrays; -// import java.util.List; -// import com.baeldung.associations.biredirectional.*; - - -// import static org.assertj.core.api.Assertions.assertThat; - -// @ExtendWith(SpringExtension.class) -// @DataJpaTest -// public class BidirectionalHibernateIntegrationTest { - -// @Autowired -// private Course courseRepository; - -// @Autowired -// private Student studentRepository; - -// @Test -// public void whenAddingStudentsToCourse_thenCourseHasStudents() { -// // given -// Student student1 = new Student(); -// student1.setName("John"); -// Student student2 = new Student(); -// student2.setName("Jane"); -// studentRepository.saveAll(Arrays.asList(student1, student2)); - -// Course course = new Course(); -// course.setName("History"); -// courseRepository.save(course); - -// // when -// List students = studentRepository.findAll(); -// course.setStudents(students); -// courseRepository.save(course); - -// // then -// Course result = courseRepository.findById(course.getId()).get(); -// assertThat(result.getStudents()).containsExactlyInAnyOrder(student1, student2); -// } - -// } - -package com.baeldung.hibernate.associations; - - -import com.baeldung.associations.biredirectional.*; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -import java.util.List; - -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/associations/DataJpaTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java deleted file mode 100644 index 184fae20c0..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/DataJpaTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.hibernate.associations; - -public @interface DataJpaTest { - -} diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java deleted file mode 100644 index 790110a6d7..0000000000 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/associations/UnidirectionalUnitTest.java +++ /dev/null @@ -1,128 +0,0 @@ - -// // import org.junit.jupiter.api.Assertions; -// // import org.junit.jupiter.api.Test; -// // import org.springframework.beans.factory.annotation.Autowired; -// // import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -// // import org.springframework.boot.test.context.SpringBootTest; -// // import org.springframework.transaction.annotation.Transactional; - -// // import java.util.Collections; -// // import java.util.List; - -// // @DataJpaTest -// // @Transactional -// // public class UnidirectionalUnitTest { - -// // @Autowired -// // private EntityManager entityManager; - -// // @Test -// // public void givenBookWithAuthor_whenSaved_thenFindBookByAuthor() { -// // // given -// // Author author = new Author(); -// // author.setName("John Doe"); - -// // Book book = new Book(); -// // book.setTitle("My Book"); -// // book.setAuthors(Collections.singleton(author)); - -// // entityManager.persist(author); -// // entityManager.persist(book); - -// // entityManager.flush(); -// // entityManager.clear(); - -// // // when -// // List booksByAuthor = entityManager.createQuery( -// // "select b from Book b join b.authors a where a.name = :name", Book.class) -// // .setParameter("name", "John Doe") -// // .getResultList(); - -// // // then -// // Assertions.assertEquals(1, booksByAuthor.size()); -// // Assertions.assertEquals(book, booksByAuthor.get(0)); -// // } -// // } - - -// package com.baeldung.hibernate.associations; - -// import com.baeldung.associations.unidirectional.*; - -// import org.junit.jupiter.api.Test; -// import org.springframework.beans.factory.annotation.Autowired; -// import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -// import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; - -// import java.util.List; -// import java.util.Set; - -// import static org.assertj.core.api.Assertions.assertThat; - -// @DataJpaTest -// public class UnidirectionalUnitTest { - -// @Autowired -// private TestEntityManager 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 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); -// } - -// } - - From d650f7ace7d112794c36f0a4c638bbe023158277 Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Sun, 30 Apr 2023 15:53:49 +0530 Subject: [PATCH 4/5] getters and setters removed from code --- .../associations/biredirectional/Course.java | 22 +++++++++++++-- .../biredirectional/Employee.java | 25 ++++++++++++++++- .../associations/biredirectional/Student.java | 27 +++++++++++++++++-- .../associations/unidirectional/Author.java | 23 ++++++++++++++++ .../associations/unidirectional/Book.java | 24 +++++++++++++++++ 5 files changed, 116 insertions(+), 5 deletions(-) diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java index aadc08c090..52d01c027c 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Course.java @@ -16,9 +16,27 @@ public class Course { inverseJoinColumns = @JoinColumn(name = "student_id")) private List students; - public List getStudents() { + public List getStudents() { return students; } - //getters and setters + 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 void setStudents(List students) { + this.students = students; + } } diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java index 92514b9b8c..1e04379ae2 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Employee.java @@ -15,5 +15,28 @@ public class Employee { @JoinColumn(name = "department_id") private Department department; - //getters and setters + + 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 Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } } diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java index a494f2475f..81e608f88e 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/biredirectional/Student.java @@ -14,7 +14,30 @@ public class Student { @ManyToMany(mappedBy = "students") private List courses; - - // getters and setters + + + 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 getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } } diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java index 6a106071a1..7e023683dc 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Author.java @@ -14,4 +14,27 @@ public class Author { @ManyToMany(fetch = FetchType.LAZY, mappedBy = "authors") private Set books; + 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 Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } } diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java index 1467a819a0..25b192fb6b 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/associations/unidirectional/Book.java @@ -1,4 +1,5 @@ package com.baeldung.associations.unidirectional; + import jakarta.persistence.*; import java.util.Set; @@ -17,4 +18,27 @@ public class Book { inverseJoinColumns = @JoinColumn(name = "author_id")) private Set authors; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } } From 4e7d1ffd1fbf8932746ee33d81510a2be3f33b4c Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Sun, 30 Apr 2023 15:58:54 +0530 Subject: [PATCH 5/5] Comments removed --- .../src/main/java/com/baeldung/HibernateUtil.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java index 8af6b12bae..26ad7e77ba 100644 --- a/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java +++ b/persistence-modules/hibernate-mapping-2/src/main/java/com/baeldung/HibernateUtil.java @@ -7,7 +7,6 @@ import org.hibernate.service.ServiceRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -// import com.baeldung.associations.unidirectional.Department; import com.baeldung.manytomany.model.Employee; import com.baeldung.manytomany.model.Project; import com.baeldung.uuids.WebSiteUser; @@ -30,7 +29,6 @@ public class HibernateUtil { configuration.addAnnotatedClass(Element.class); configuration.addAnnotatedClass(Reservation.class); configuration.addAnnotatedClass(Sale.class); - // configuration.addAnnotatedClass(Department.class); configuration.configure("manytomany.cfg.xml"); LOGGER.debug("Hibernate Annotation Configuration loaded");