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?

nbk
  • 45,398
  • 8
  • 30
  • 47
  • 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" – g00se Apr 26 '22 at 11:47
  • @Daniel Vilas-Boas has of course gone straight to the possible solution – g00se Apr 26 '22 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. – RealSkeptic Apr 26 '22 at 11:53
  • The serialization was the way I was interested in trying this –  Apr 26 '22 at 12:02
  • Can you briefly explain to me why Serializing it is bad practice please –  Apr 26 '22 at 12:09
  • "_why Serializing is bad practice_": A summary giving some reasons can be found in the Josh Bloch _Effective Java_ book (see item 74) - I found a reproduction [here](http://jtechies.blogspot.com/2012/07/item-74-implement-serializable_07.html). But you can also see similar comments in various SO questions - here is [one](https://stackoverflow.com/questions/15557783/effective-java-item-no-74on-serialization-implement-serializable-judiciously). – andrewJames Apr 26 '22 at 13:30

1 Answers1

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;
}
  • 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 –  Apr 26 '22 at 12:01
  • 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? –  Apr 26 '22 at 12:21
  • 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. – Daniel Vilas-Boas Apr 26 '22 at 14:31
  • A suggestion is all I was looking for thank you for helping I will try a linking table –  Apr 27 '22 at 11:39
  • sorry im new to the site how do i accept –  May 01 '22 at 14:02
  • usually there is an option to accept the reply as the answer, don't you see it? if that's the case, feel free to just upvote – Daniel Vilas-Boas May 01 '22 at 19:09
  • found it its the green tick. thank you again –  May 02 '22 at 17:16