0

I have a Student class:

public class Student implements Serializable{
    //each student has an arraylist of modules

    private String name = null;     //students name
    private int ID = 0;             //students ID number
    private String DOB = null;      //Students Date of Birth

    private ArrayList <Module> modules;         //creating the students module arraylist

public Student(String name,int ID, String DOB) {
    this.setName(name);
    this.setID(ID);
    this.setDOB(DOB);
    setModules(new ArrayList <Module> ());          //initialising the arraylist
}

Each student has many modules and the grade they got in each of these modules. I need to write these student objects to a Mysqln database but how would I store the arraylist of modules each student has in the student table?

6
  • Well you're really asking "How do I reinvent OR mapping software?". To that (probably) the correct answer is "You don't!. You use OR mapping software" Commented Apr 26, 2022 at 11:47
  • @Daniel Vilas-Boas has of course gone straight to the possible solution Commented Apr 26, 2022 at 11:50
  • I think perhaps we should begin with the basics: You can't save a Java object to database. You can either serialize it (bad idea, as it's opaque), or represent each class as a table, and the connections between them as a table, where the rows are the object instances and the columns are the values of their fields. To do that, you use an OR mapping software. Commented Apr 26, 2022 at 11:53
  • The serialization was the way I was interested in trying this Commented Apr 26, 2022 at 12:02
  • Can you briefly explain to me why Serializing it is bad practice please Commented Apr 26, 2022 at 12:09

1 Answer 1

1

Depends on how you intend to design your application, in case you'd like to follow a many to many relationship (each student has N modules and each module is being done by multiple students).

This kind of mapping can be done in two ways, unidirectional or bidirectional, it depends on how you plan to manipulate the entities or when there is an entity that is stronger.

There is no way you can store a list of modules inside the same student table unless you go to another kind of database (Mysql is relational) or would like to see a list of comma separated strings in a textual/blob column, my opinion is that this is a denormalized poor implementation.

Here is a code snippet you can try that will create a intermediary table to store the relationship between the entities.

@Entity
@Table(name="student")
class Student {

private long studentId;

@ManyToMany
    @JoinTable(name="student_modules", joinColumns=
    {@JoinColumn(name="student_id")}, inverseJoinColumns=
      {@JoinColumn(name="module_id")})
List<Module> modules;

}

@Entity
@Table(name="notebook")
public class Module {

 private long moduleId;
}
Sign up to request clarification or add additional context in comments.

7 Comments

The application will work in a way where the user can select a student then add modules and grades to that particular student and then view all of the students details on sort their grades low-high. So the modules would get populated In this way they have no ID just a name and grade
How would I store the grade each student got for each module? since a single module has many students and a student has many modules. would I need a moduleGrade table for each student?
I see grades as a link between module and student, and thus could be stored in the same intermediary table or in a new one if you want. In that cases, you may need to create a custom entity to store the foreign keys and the custom data (grades in your case). Please note this is a suggestion and not the only way to model your application.
A suggestion is all I was looking for thank you for helping I will try a linking table
sorry im new to the site how do i accept
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.