MiaMills / Student-Behaviour-Report

This project is a RESTful API for tracking student behavior, built with Java and Spring Boot. It supports CRUD operations and data filtering, managing data in a MySQL database with robust exception handling.
GNU General Public License v3.0
0 stars 0 forks source link

Define the Key Entities (as part of API development) #9

Open MiaMills opened 4 days ago

MiaMills commented 4 days ago

Identify the main entities I'll be working with based on my requirements; this includes classes for Student, Teacher, and Report. Each entity should have a Java class with attributes representing fields in my database (like Student ID, Student Name, Student Email, Subject, Class, Teacher Comment).

Defining entities solves issues related to project organisation, maintainability, database design, data relationships, and project clarity. It’s an essential step that allows I build a well-structured, efficient, and scalable Student Behaviour Report project.

Additionally, clear entities allow me to:

  1. design REST APIs elements that correspond to each entity, making it easy for other developers (or my in the future) to understand and work with my project.
  2. know exactly what data needs to be collected, processed, and reported, which helps keep the project on track and reduces the chance of missing key features.
  3. to map relationships between them, like linking students to teachers or reports. This makes it easier to perform tasks such as generating reports on student behaviour by linking data from different entities.
  4. create Java classes that encapsulate relevant data and behaviour for each concept (like Student and Teacher). This supports OOP principles like encapsulation, inheritance, and polymorphism, making your code more modular and flexible.
  5. better-organised code with classes and fields that represent real-world concepts, making my project easier to understand, update, and expand.
MiaMills commented 2 days ago

IDs were already created for each entity; use autogenerated keys (using @autogenerated annotation) instead. However, can update a database to remove the studentId field if it's not needed. Here’s a step-by-step guide on how to do it, along with considerations for both automatic schema management (using JPA) and manual schema changes.

  1. Updating the Database Schema Automatically with JPA (if allowed) If your database is set to allow JPA/Hibernate to automatically manage the schema, you can configure JPA to drop and recreate the table without the studentId field.

In the application.properties or application.yml file, set the following properties:

spring.jpa.hibernate.ddl-auto=update

This setting tells JPA to automatically adjust the schema to match your entity class.

Modify the Entity Class: Remove the studentId field from the Student class:

After making these changes, run your application. Hibernate will automatically remove the studentId field from the table if you are using spring.jpa.hibernate.ddl-auto=update.

Note: This method might work differently depending on the database system, and it can be risky in a production environment as it may result in data loss or corruption if not used carefully.

Yes, you can update your database to remove the studentId field if it's not needed. Here’s a step-by-step guide on how to do it, along with considerations for both automatic schema management (using JPA) and manual schema changes.

  1. Updating the Database Schema Automatically with JPA (if allowed) If your database is set to allow JPA/Hibernate to automatically manage the schema, you can configure JPA to drop and recreate the table without the studentId field.

In your application.properties or application.yml file, set the following properties: properties Copy code spring.jpa.hibernate.ddl-auto=update This setting tells JPA to automatically adjust the schema to match your entity class.

Modify the Entity Class: Remove the studentId field from the Student class:

java Copy code import javax.persistence.Entity;

@Entity public class Student {

private final String studentName;
private final String studentClass;
private final String teacherComment;

// Constructor, getters, etc.

} After making these changes, run your application. Hibernate will automatically remove the studentId field from the table if you are using spring.jpa.hibernate.ddl-auto=update.

Note: This method might work differently depending on the database system, and it can be risky in a production environment as it may result in data loss or corruption if not used carefully.

  1. Manual Approach: Updating the Database Schema Directly If automatic schema management is disabled or you prefer a safer, manual approach, you can remove the studentId field directly in your database.

Step-by-Step Guide for Manual Removal Connect to the Database: Use a database management tool, such as MySQL Workbench, phpMyAdmin, or the MySQL CLI.

Use SQL to Alter the Table: Run an SQL ALTER TABLE command to drop the studentId column.

sql Copy code ALTER TABLE student DROP COLUMN studentId; Verify the Change: Check the table structure to make sure studentId has been removed.

Example SQL Command Breakdown ALTER TABLE student: This specifies the student table, where the studentId column exists. DROP COLUMN studentId: This part removes the studentId column from the student table. Important Considerations Data Backup: It’s a good idea to back up your data before altering the schema. Consistency: Make sure no other tables or parts of your application rely on the studentId field. After Manual Update Once you have removed studentId from the database, you can also remove it from your Student class as shown above.

Final Note Once studentId is removed from both the entity class and the database, the Student entity will only contain studentName, studentClass, and teacherComment. Make sure your code and any dependent classes/methods are updated accordingly.