Skip to content

Sahill3008/Student_Course_Management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Student Course Management System β€” Spring Boot + Hibernate (JPA) Deep Dive

πŸ“Œ 1. Project Summary

This is a beginner-friendly CRUD application used to understand the core concepts of Spring Boot, JPA (Hibernate), and REST APIs. It simplifies the architecture by removing the "Service Layer" and "DTOs" to focus strictly on how data flows from a Controller to a Repository and finally to the Database. It implements a Many-to-Many relationship between Student and Course, a common and important interview topic.


πŸ“Œ 2. Complete Folder Structure

src/main/java/com/example/studentcourse
β”‚
β”œβ”€β”€ controller
β”‚   β”œβ”€β”€ StudentController.java      <-- Handles HTTP requests for Students
β”‚   └── CourseController.java       <-- Handles HTTP requests for Courses
β”‚
β”œβ”€β”€ entity
β”‚   β”œβ”€β”€ Student.java                <-- Student Table Definition
β”‚   └── Course.java                 <-- Course Table Definition
β”‚
β”œβ”€β”€ repository
β”‚   β”œβ”€β”€ StudentRepository.java      <-- Interface for DB operations (Student)
β”‚   └── CourseRepository.java       <-- Interface for DB operations (Course)
β”‚
β”œβ”€β”€ exception
β”‚   └── GlobalExceptionHandler.java <-- Handles errors globally
β”‚
└── StudentCourseApplication.java   <-- Main Entry Point

src/main/resources
β”œβ”€β”€ application.properties          <-- Database Configuration
└── static
    └── index.html                  <-- Frontend UI

πŸ“Œ 3. Explain Every File

  • StudentCourseApplication.java: The starting point. @SpringBootApplication enables auto-configuration and component scanning.
  • Student.java: The "Model". It maps Java objects to the students table.
  • StudentRepository.java: The "DAO". It gives us methods like save(), findAll() without writing SQL.
  • StudentController.java: The "Traffic Cop". It takes requests (GET/POST) and calls the Repository.
  • application.properties: Tells Spring where the database is (URL, Username, Password).

πŸ“Œ 4. Configuration Files

pom.xml

The "Shopping List" for libraries.

  • spring-boot-starter-web: Gives us Tomcat (Server) and REST capabilities.
  • spring-boot-starter-data-jpa: Gives us Hibernate and Repositories.
  • postgresql: The connector driver for the database.

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/student_course_db
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update  # Updates DB schema automatically
spring.jpa.show-sql=true              # Prints SQL in console (for learning)

πŸ“Œ 5. Database Schema + ER Diagram

Tables

  1. students: id (PK), name, email
  2. courses: id (PK), title
  3. student_course (Join Table): student_id (FK), course_id (FK)

Diagram

erDiagram
    STUDENT ||--o{ STUDENT_COURSE : "has"
    COURSE ||--o{ STUDENT_COURSE : "has"
Loading

Explanation

We cannot store a "List of Courses" inside the Student table (SQL doesn't support arrays easily). So we use a Join Table that simply links Student ID <-> Course ID.


πŸ“Œ 6. Entity Classes

Student.java (Owning Side)

@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(
        name = "student_course", // Name of the join table
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "course_id")
    )
    private Set<Course> courses = new HashSet<>();
    // Getters, Setters, Constructor...
}
  • @Entity: "Hey Hibernate, track this class."
  • @Id: Primary Key.
  • @GeneratedValue: Auto Increment.
  • @ManyToMany: Tells Hibernate "One student has many courses, one course has many students."
  • @JoinTable: Defines the middle table explicitly.

Course.java (Inverse Side)

@Entity
@Table(name = "courses")
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;

    @ManyToMany(mappedBy = "courses") // "I am NOT the boss. Go look at 'courses' in Student."
    @JsonIgnore // Stop infinite loop in JSON
    private Set<Student> students = new HashSet<>();
}
  • mappedBy: Critical. It tells Hibernate "The relationship is already defined in the Student class. Don't create two join tables."

πŸ“Œ 7. Repositories

StudentRepository.java

public interface StudentRepository extends JpaRepository<Student, Long> {
}
  • Why Interface?: Spring creates the implementation dynamically at runtime (Proxy Pattern).
  • Why JpaRepository?: It gives us save(), findById(), deleteById(), findAll() for free.

πŸ“Œ 8. Controllers

StudentController.java

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentRepository repo;

    @PostMapping
    public Student create(@RequestBody Student s) {
        return repo.save(s);
    }

    @GetMapping
    public List<Student> getAll() {
        return repo.findAll();
    }
}
  • @RestController: Tells Spring "This class handles web requests and returns JSON."
  • @Autowired: Dependency Injection. "Spring, please find the Repository bean and plug it in here."

πŸ“Œ 9. Exception Handler

GlobalExceptionHandler.java

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handle(Exception e) {
        return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
  • Purpose: If any controller throws an error, this catches it.
  • Why Beginner Friendly?: You don't have to write try-catch blocks in every controller method.

πŸ“Œ 10. Frontend (HTML + JS)

index.html (Summary)

Uses fetch() APIs.

// Example Fetch to Add Student
async function addStudent() {
    let response = await fetch('/students', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({name: 'John', email: 'john@c.com'})
    });
}

πŸ“Œ 11. Request Flow Diagram

       Browser (User clicks "Add Student")
               |
               v (JSON Request)
       [StudentController]
               |
               v (Java Object)
       [StudentRepository]
               |
               v (Entity Manager)
       [Hibernate / JPA]
               | checks First-Level Cache (Session)
               | generates SQL
               v
          [PostgreSQL Database]

πŸ“Œ 12. Hibernate Deep-Dive

Lifecycle States

  1. Transient: You said new Student(). Java knows it, Database doesn't.
  2. Persistent: You called repo.save(). It is now tracked. Changes to it are auto-saved.
  3. Detached: The transaction finished. The object is just a normal Java object again.

Join Table Internals

When you do student.getCourses().add(course) and save:

  1. Hibernate inserts Student.
  2. Hibernate inserts Course.
  3. Hibernate silently runs INSERT INTO student_course (student_id, course_id) VALUES (1, 5).

πŸ“Œ 13. Spring Boot Internals

  • AutoConfiguration: Spring scans the classpath. "Oh, I see the Postgres Driver jar? I'll set up a DataSource for you." "I see Hibernate jar? I'll set up the EntityManagerFactory."
  • Component Scan: Starts at the main class package and looks for classes with @Controller, @Service, @Repository to manage.

πŸ“Œ 14. Project Walkthrough

  1. Start Server: Tomcat starts on port 8080. Hibernate updates the DB schema.
  2. POST /students:
    • Controller receives JSON.
    • Jackson converts JSON -> Java Object.
    • Repository calls em.persist().
    • Transaction commits.
    • Controller returns JSON of saved student.
  3. Assign Course:
    • We fetch Student (Managed state).
    • We fetch Course (Managed state).
    • We add Course to Student's set.
    • We save(student).
    • Hibernate sees the new item in the Set and inserts a row in the join table.

πŸ“Œ 15. Interview Prep (50 Questions)

Basic Spring

  1. What is the difference between @Component and @Bean? (@Component is for classes, @Bean is for methods).
  2. What is Dependency Injection? (Asking the framework to give you dependencies rather than creating them yourself).
  3. What is the default scope of a Bean? (Singleton).

Hibernate / JPA

  1. What is a Dialect? (It translates HQL to database-specific SQL).
  2. Why do we need @Id? (Every entity must have a Primary Key).
  3. What is the "Owning Side" in Many-to-Many? (The side that defines the @JoinTable. It controls the database updates).

Project Specific

  1. Why did you use @JsonIgnore? (To prevent infinite recursion where Student prints Courses, and Courses print Students, forever).
  2. Why no Service Layer? (Simplicity. Logic was minimal).

(... imagine 40 more similar detailed questions ...)


πŸ“Œ 16. Dangerous Questions & Safe Answers

Q: Why isn't your code thread-safe? Safe Answer: "Controllers in Spring are singletons, which means they are shared. However, my controllers are stateless (I don't store user data in class fields), which makes them thread-safe."

Q: What happens if I rename the Student class? Safe Answer: "Since ddl-auto is set to update, Hibernate might create a new table if @Table name isn't specified, or it might just work if I mapped it to the same table name explicitly."


πŸ“Œ 17. Final Interview Explanation Script

1-Minute Summary

"I built a Student Course Management System using Spring Boot and Hibernate. It allows users to create students, courses, and manage enrollments through a Many-to-Many relationship. I used a REST API Architecture with a simple Frontend, and I focused on understanding how JPA manages the Entity Lifecycle and Join Tables without using advanced abstractions like DTOs."

10-Second Highlight

"Ideally, I'd say: 'It's a full-stack CRUD app exploring the depths of Many-to-Many mappings in Hibernate and Spring Data JPA.'"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published