Hibernate Associations
This commit is contained in:
		
							parent
							
								
									82f9d6a500
								
							
						
					
					
						commit
						1767ea3cdc
					
				| @ -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<Student> 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<Employee> 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<Student> result = course.getStudents(); |  | ||||||
| 
 |  | ||||||
|         // then |  | ||||||
|         assertAll("course students", |  | ||||||
|             () -> assertEquals(2, result.size()), |  | ||||||
|             () -> assertTrue(result.contains(student1)), |  | ||||||
|             () -> assertTrue(result.contains(student2)) |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,5 +0,0 @@ | |||||||
| package com.baeldung.hibernate.associations; |  | ||||||
| 
 |  | ||||||
| public @interface DataJpaTest { |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -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<Book> 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); |  | ||||||
| //     } |  | ||||||
| 
 |  | ||||||
| // } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user