From 1767ea3cdc4982644331c213abefc751d64bc20f Mon Sep 17 00:00:00 2001 From: Kingsley Amankwah Date: Thu, 27 Apr 2023 05:10:50 +0530 Subject: [PATCH] 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); -// } - -// } - -