พัฒนา web application ด้วย Java

 Spring Boot applications leverage JPA (Java Persistence API) with Hibernate as the default implementation to manage relationships between entities. Here are examples for one-to-one, one-to-many, and many-to-many relationships:

1. One-to-One Relationship
A one-to-one relationship implies that one instance of an entity is associated with exactly one instance of another entity.
Example: User and UserProfile
Java
// User.java@Entitypublic class User {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String username;    @OneToOne(cascade = CascadeType.ALL)    @JoinColumn(name = "user_profile_id", referencedColumnName = "id")    private UserProfile userProfile;    // Getters and Setters}// UserProfile.java@Entitypublic class UserProfile {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String address;    private String phoneNumber;    @OneToOne(mappedBy = "userProfile") // "mappedBy" indicates the owning side    private User user;    // Getters and Setters}
2. One-to-Many / Many-to-One Relationship
A one-to-many relationship means one entity can be associated with multiple instances of another entity. The inverse is a many-to-one relationship.
Example: Department and Employee
Java
// Department.java (One side)@Entitypublic class Department {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String name;    @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)    private List<Employee> employees = new ArrayList<>();    // Getters and Setters}// Employee.java (Many side)@Entitypublic class Employee {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String name;    @ManyToOne    @JoinColumn(name = "department_id") // Foreign key in Employee table    private Department department;    // Getters and Setters}
3. Many-to-Many Relationship
A many-to-many relationship indicates that multiple instances of one entity can be associated with multiple instances of another entity. This is typically managed through a join table.
Example: Student and Course
Java
// Student.java@Entitypublic class Student {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String name;    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})    @JoinTable(        name = "student_course",        joinColumns = @JoinColumn(name = "student_id"),        inverseJoinColumns = @JoinColumn(name = "course_id")    )    private Set<Course> courses = new HashSet<>();    // Getters and Setters}// Course.java@Entitypublic class Course {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String title;    @ManyToMany(mappedBy = "courses")    private Set<Student> students = new HashSet<>();    // Getters and Setters}
Key Annotations and Concepts:
  • @EntityMarks a class as a JPA entity, mapping it to a database table.
  • @IdDesignates the primary key of an entity.
  • @GeneratedValueConfigures the primary key generation strategy.
  • @OneToOne@OneToMany@ManyToOne@ManyToManyDefine the type of relationship.
  • @JoinColumnSpecifies the foreign key column in the owning side of the relationship.
  • mappedByUsed on the inverse side of a bidirectional relationship to indicate the field in the owning entity that manages the relationship.
  • @JoinTableUsed in many-to-many relationships to define the join table and its columns.
  • CascadeTypeSpecifies how persistence operations (e.g., persist, merge, remove) should cascade from one entity to its related entities.
  • orphanRemovalWhen set to true with @OneToMany, it automatically removes child entities from the database when they are no longer referenced by the parent.
Reviewed by Mr.Boonchai on พฤศจิกายน 18, 2568 Rating: 5

ไม่มีความคิดเห็น:

Thank you

ขับเคลื่อนโดย Blogger.