-1

I have two classes: Student and Group.

Student attributes:

private String registrationNumber;
private String firstName;
private String lastName;
private Date dateOfBirth;
private String password;
private Groupe groupe;
private List<Grades> studentGrades;

Student Constructor:


public Student(String registrationNumber, String firstName, String lastName, Date dateOfBirth, Groupe groupe,String password) {
    super();
    this.registrationNumber = registrationNumber;
    this.firstName = firstName;
    this.lastName = lastName;
    this.dateOfBirth = dateOfBirth;
    this.groupe = groupe;
    this.password=password;
}

Group attributes:

private int idGroup;
private String nameGroup;
private List<Student> students;
private List<Subject> subjects;

Its constructor takes only idGroup and nameGroup.

When registering, a student will select a group from a drop down menu (Swing interface) which will then call the addStudent function (controller to DAO).

public boolean addStudent( Student s) throws SQLException {
        myStatement=myConnection.getMyConnection().createStatement();
        String request="insert into student values('"+s.getRegistrationNumber()+"','"+s.getFirstName()+"','"+s.getLastName()+"','"+s.getDateOfBirth()+"','"+s.getPassword()+"')";
        return myStatement.executeUpdate(request)>0;
    }

Which is currently missing the group attribute.

How should I translate the group attribute (and by extension all other associations) from OOP to a relational format?

Should I drop these association from student and use an int attribute that represents a foreign key which refrences the Group table's primary key and rely on joins for the List (1..n associations)?

6
  • Basically, yes, the relationship model suggests that Student references a Group through a foreign key. But no, not int. In OOP, this is a reference to a Group. Don't use any special fields or properties. to translate to the relational database. Your data types should remain pure OOP. Keep to the S.P.O.T. principle (Single Point of Truth). Commented yesterday
  • 3
    (not necessarily in order of importance) a. Don't use Date - it's obsolete b. Don't reinvent the wheel of OR mapping c. Use try-with-resources for your DB objects (but see b. too) d. Don't be Little Bobby Tables Commented yesterday
  • 1
    I did not say this. Please read carefully: 1) Do use foreign key in the database, 2) Do not represent this key in your TypeScript, Kotlin, or other code in any additional way. The code should remain pure OOP. You have enough information to put the reference in your database. In particular, in the first sample, this is the line private Groupe groupe; it is the real equivalent of the relational database relation. Commented yesterday
  • 2
    Take care to avoid SQL injection vulnerability (to clarify a point g00se alluded to) Commented yesterday
  • 1
    I think the OP may be asking "How to I write this information back to the database if I don't save the foreign key." I'm having trouble with that too. I guess one technique would be to read the key back just before the write, but that also might be seen as inefficient. @SergeyAKryukov Commented yesterday

1 Answer 1

0

This is the solution I have come up with after implementing everyone's input in case someone else runs into a similar problem.

Starting with the student table:

registration Number firstName lastName dateofBirth password idGroup

The updated addStudent function with SQL injection safety (Thanks for the clarification Tim Moore) :

public boolean addStudent( Student s) throws SQLException {
        String request="INSERT INTO student values (?,?,?,?,?,?);";
        PreparedStatement pst=myConnection.getMyConnection().prepareStatement(request);
        pst.setString(1, s.getRegistrationNumber());
        pst.setString(2, s.getFirstName());
        pst.setString(3, s.getLastName());
        pst.setDate(4, s.getDateOfBirth());
        pst.setString(5, s.getPassword());
        pst.setInt(6, s.getGroup().getIdGroup());
        return pst.executeUpdate()>0;
    }

s.getGroup().getIdGroup() saves the student's group to the foreign key idGroup.

Sign up to request clarification or add additional context in comments.

1 Comment

Better. But you want try (PreparedStatement pst=myConnection.getMyConnection().prepareStatement(request)) { // the rest}; and pst.setObject(4, s.getDateOfBirth());, where that attribute is of type java.time.LocalDate

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.