Last active
March 12, 2025 23:14
-
-
Save hakkm/8c5bfce3318a761a7dbe37549fb41897 to your computer and use it in GitHub Desktop.
OneToMany using a join table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello.demo.model; | |
import jakarta.persistence.Entity; | |
import jakarta.persistence.GeneratedValue; | |
import jakarta.persistence.GenerationType; | |
import jakarta.persistence.Id; | |
import java.io.Serializable; | |
@Entity | |
public class Course { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String name; | |
private String description; | |
public Course() { | |
} | |
public Course(String name, String description) { | |
this.name = name; | |
this.description = description; | |
} | |
@Override | |
public String toString() { | |
return "Course{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
", description='" + description + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello.demo.model; | |
import jakarta.persistence.*; | |
import java.util.HashSet; | |
import java.util.Set; | |
@Entity | |
@Table(name = "students") | |
public class Student { | |
@Id | |
private String cne; | |
private String name; | |
private String email; | |
private int age; | |
@OneToMany(cascade = CascadeType.ALL) | |
private Set<Course> courses = new HashSet<>(); | |
public Student(String cne, String name, String email, int age) { | |
this.cne = cne; | |
this.name = name; | |
this.email = email; | |
this.age = age; | |
} | |
public Student() { | |
System.out.println("Student created"); | |
} | |
public Set<Course> getCourses() { | |
return courses; | |
} | |
public void setCourses(Set<Course> courses) { | |
this.courses = courses; | |
} | |
public String getCne() { | |
return cne; | |
} | |
public void setCne(String cne) { | |
this.cne = cne; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public int getAge() { | |
return age; | |
} | |
@Override | |
public String toString() { | |
return "Student{" + | |
"cne='" + cne + '\'' + | |
", name='" + name + '\'' + | |
", email='" + email + '\'' + | |
", age=" + age + | |
'}'; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public static class StudentBuilder { | |
private final Student student = new Student(); | |
public StudentBuilder cne(String cne) { | |
student.setCne(cne); | |
return this; | |
} | |
public StudentBuilder name(String name) { | |
student.setName(name); | |
return this; | |
} | |
public StudentBuilder email(String email) { | |
student.setEmail(email); | |
return this; | |
} | |
public StudentBuilder age(int age) { | |
student.setAge(age); | |
return this; | |
} | |
public Student build() { | |
return student; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment