-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)?

Shadow's user avatar
Shadow
34.5k10 gold badges67 silver badges76 bronze badges
askedNov 28 at 23:47
Mohamed Selmi's user avatar
6
  • Basically, yes, the relationship model suggests thatStudent references aGroup through a foreign key. But no, notint. In OOP, this is areference to aGroup. 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).CommentedNov 28 at 23:59
  • 4
    (not necessarily in order of importance)a. Don't useDate - it's obsoleteb. Don't reinvent the wheel of OR mappingc. Use try-with-resources for your DB objects (but see b. too)d. Don't beLittle Bobby TablesCommentedNov 29 at 0:10
  • 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 anyadditional 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 lineprivate Groupe groupe; it is the real equivalent of the relational database relation.CommentedNov 29 at 0:54
  • 3
    Take care to avoidSQL injection vulnerability (to clarify a point g00se alluded to)CommentedNov 29 at 2:25
  • 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. @SergeyAKryukovCommentedNov 29 at 3:15

1 Answer1

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 NumberfirstNamelastNamedateofBirthpasswordidGroup

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 keyidGroup.

answeredNov 29 at 20:13
Mohamed Selmi's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

Better. But you wanttry (PreparedStatement pst=myConnection.getMyConnection().prepareStatement(request)) { // the rest}; andpst.setObject(4, s.getDateOfBirth());, where that attribute is of typejava.time.LocalDate

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.