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)?
- Basically, yes, the relationship model suggests that
Studentreferences aGroupthrough 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).Sergey A Kryukov– Sergey A Kryukov2025-11-28 23:59:57 +00:00CommentedNov 28 at 23:59 - 4(not necessarily in order of importance)a. Don't use
Date- 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 Tablesg00se– g00se2025-11-29 00:10:52 +00:00CommentedNov 29 at 0:10 - 1I 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 line
private Groupe groupe;it is the real equivalent of the relational database relation.Sergey A Kryukov– Sergey A Kryukov2025-11-29 00:54:55 +00:00CommentedNov 29 at 0:54 - 3Take care to avoidSQL injection vulnerability (to clarify a point g00se alluded to)Tim Moore– Tim Moore2025-11-29 02:25:16 +00:00CommentedNov 29 at 2:25
- 1I 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. @SergeyAKryukovmarkspace– markspace2025-11-29 03:15:31 +00:00CommentedNov 29 at 3:15
1 Answer1
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 keyidGroup.
1 Comment
try (PreparedStatement pst=myConnection.getMyConnection().prepareStatement(request)) { // the rest}; andpst.setObject(4, s.getDateOfBirth());, where that attribute is of typejava.time.LocalDateExplore related questions
See similar questions with these tags.
