# Schema design

### <mark style="background-color:red;">Q How to read relationships in schema design ?</mark>&#x20;

### <mark style="color:green;background-color:green;">One To Many Relationship</mark>

Take an example :: Mother and Child

#### <mark style="color:blue;">Mother Side</mark>

* 1 mother can have one children only ? ❌
* 1 mother can have many children?  ✅
* 1 mother -> many child
* here we find <mark style="background-color:orange;">child (many).</mark>

#### <mark style="color:blue;">Child Side</mark>

* 1 child can be associated with many mothers ?  ❌
* 1 child can be associated with one only mother ? ✅
* 1 child -> one mother
* here we find <mark style="background-color:purple;">mother (one)</mark>

#### <mark style="color:blue;">Result</mark>&#x20;

<mark style="background-color:purple;">mother (one)</mark> <mark style="color:blue;">--</mark><mark style="background-color:orange;">child (many).</mark>

#### <mark style="color:blue;">Annotations</mark>

Fk :: annotation @ManyToOne <mark style="color:blue;">(child side)</mark>&#x20;

inverse side :: annotation @OneToMany <mark style="color:blue;">(mother side)</mark>

#### CODE :: ONE TO MANY 🌈

```java
/* ==== Child (FK Side) ===== */
public class Child {
 @ManyToOne
 @joinColumn(name="mother_id")
 private Mother motherRef;
}
```

```java
/* ==== Mother (Inverse Side) ===== */
// annotation tells the hibernate fetch the related child for "motherRef" 
// So it will see the motherRef inside Child class and 
// and fetch lazyly when getter is called (fetch on demand) as by default 
// @OneToMany is Lazy Fetch 
public class Mother {
 @OneToMany(mappedBy="motherRef")
 List<Child> child = new ArrayList<>();
}


```

#### CODE :: ONE TO MANY  EXPLAIN (Contd ..)

![](https://1745304778-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfN8LrvCfmacwBtoclAjJ%2Fuploads%2F2hL6hnpPn0MSqtyBOwoW%2Fimage.png?alt=media\&token=15d68ae1-aa5e-48b9-820e-29e23935b957)

### <mark style="color:green;background-color:green;">Many To Many Relationship</mark>

Take an example :: Course and Student Relationship

#### <mark style="color:blue;">Course Side</mark>

* 1 Course is enrolled by one student only ? ❌
* 1 Course can be enrolled by one many students ? ✅
* 1 Course -> <mark style="background-color:orange;">many Students</mark>
* here we find students side (many)

#### <mark style="color:blue;">Student Side</mark>

* 1 student can be enrolled in one course only ? ❌
* 1 student can be enrolled in many courses ? ✅
* 1 Student -> <mark style="background-color:purple;">many Courses</mark>
* here we find Course side (many)

Hence the resultant is many to many relationship

#### CODE :: MANY TO MANY 🌈

{% hint style="warning" %}
Remove Circular dependency in your controller class in manyToMany case

i.e User.getRoles().setUsers(null);  and vice versa
{% endhint %}

```java
/* ==== Role Side ===== */
public class Role {
  @ManyToMany(
  fetch = FetchType.LAZY,
  cascade = {
  CascadeType.PERSIST,
  CascadeType.MERGE, 
  CascadeType.DETACH,
  CascadeType.REFRESH
  })
  @JoinTable(
  name = "course_student", // joinTableName
  joinColumns = @JoinColumn(name = "role_id"), // fk_this_Table
  inverseJoinColumns = @JoinColumn(name = "user_id") // fk_inverse_Table
  )
  private List<User> users = new ArrayList<>(); // inverse_Table_List
}
```

```java
/* ==== Role Side ===== */
public class User {
  @ManyToMany(
  fetch = FetchType.LAZY,
  cascade = {
  CascadeType.PERSIST,
  CascadeType.MERGE, 
  CascadeType.DETACH,
  CascadeType.REFRESH
  })
  @JoinTable(
  name = "course_student", // joinTableName
  joinColumns = @JoinColumn(name = "user_id"), // fk_this_Table
  inverseJoinColumns = @JoinColumn(name = "role_id") // fk_inverse_Table
  )
  private List<Role> users = new ArrayList<>(); // inverse_Table_List
}

```

####

####

####
