JPA/Hibernate Associations
This commit is contained in:
parent
fbc3b5920b
commit
00028b8b19
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.association;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Department {
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "department")
|
||||||
|
private List<Employee> employees;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "department_id")
|
||||||
|
private Department department;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Student {
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "students")
|
||||||
|
private List<Course> courses;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Course {
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
@JoinTable(name = "course_student",
|
||||||
|
joinColumns = @JoinColumn(name = "course_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "student_id"))
|
||||||
|
private List<Student> students;
|
||||||
|
|
||||||
|
}
|
|
@ -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<Employee> 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<Author> authors;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Author {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
|
@ -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<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))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue