> For the complete documentation index, see [llms.txt](https://mohsin-ejaz.gitbook.io/schema-design/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mohsin-ejaz.gitbook.io/schema-design/schema-design.md).

# 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 ..)

![](/files/WVLOZElLXBIUTg8MZITY)

### <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
}

```

####

####

####


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mohsin-ejaz.gitbook.io/schema-design/schema-design.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
