Merge pull request #13875 from KingsleyAmankwah/master

JPA/Hibernate Associations
This commit is contained in:
davidmartinezbarua 2023-05-30 13:55:07 -03:00 committed by GitHub
commit b0799740ad
9 changed files with 318 additions and 0 deletions

View File

@ -0,0 +1,42 @@
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<Student> students;
public List<Student> getStudents() {
return students;
}
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<Student> students) {
this.students = students;
}
}

View File

@ -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<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
}

View File

@ -0,0 +1,42 @@
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;
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;
}
}

View File

@ -0,0 +1,43 @@
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<Course> courses;
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<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}

View File

@ -0,0 +1,40 @@
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<Book> 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<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
}

View File

@ -0,0 +1,44 @@
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<Author> 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<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
}

View File

@ -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<Employee> employees;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.associations.unidirectional;
import jakarta.persistence.*;
@Entity
public class ParkingSpot {
@Id
private Long id;
}