-
Notifications
You must be signed in to change notification settings - Fork 0
Objects_Relations_en
The base of any relation between objects in Sofof is the identifier ID which exists in the package org.sofof. This class specifies the location of an object in the database (binding name + class + unique identifying number). The relations depend on this object in pointing to objects that related to it. Sofof will search on an instance of the ID class in the fields of the object you want to save and will generate a new instance of it if the field's value is null, but if the ID field has a value then it will check if there is an equal value to it (same binding name, class and identifying number) in the other already existing objects and will throw an exception if there is one. It's important to understand that you shouldn't create an instance of the ID on your code. The following class will show you how to add an ID field to your class:
public class Student{
private ID id;
private String name;
//code...
}After trying to save your object that has a null ID field in the database, Sofof will generate a new instance of the ID and will save it in that field. So, you have to read your object after saving it in the database to have an object with a valid ID instance. To do that you should have a good implementation of the equals method in your object class that doesn't include the ID field in the conditions of the equation if one of them has a null value. Bind command offers an easy way to bind and read the same object from the database (this will not work if you didn't implement the equals method in the right way) by sending it as a query to be executed just after executing it as an executable command to bind your object (this way will work if you tried to save more than one object). After that, you can assign the object that has a valid ID field as a field of another object that could be saved in the database and if you wanted to read that object from the database then sofof will search for any fields or fields fields etc of that object to see if there is a field that has a valid ID instance and if there is a one it will use the ID instance to reach the object that is the ID is pointing to it. This example will show you how to create a relation between a Teacher object and a Course object:
public class Teacher implements Serializable{
private static final long serialVersionUID = 124530;
private ID id;
private String name;
public Teacher(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Teacher other = (Teacher) obj;
if (id != null && other.id != null && !id.equals(other.id)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
}
public class Course implements Serializable {
private static final long serialVersionUID = 5465235;
private ID id;
private String name;
private Teacher teacher;
public Course(String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
}
//getters and setters
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Course other = (Course) obj;
if (id != null && other.id != null && !id.equals(other.id)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.teacher, other.teacher)) {
return false;
}
return true;
}
}
//code after all preparations for database and connections
Teacher samer = new Teacher("Samer");
Bind teacherBind = new Bind(samer);
session.execute(teacherBind);
samer = (Teacher)session.query(teacherBind).get(0);
Course math = new Course("Math", samer);
session.execute(new Bind(math));
samer.setName("Majed");
session.execute(new Update(samer).set(samer));
System.out.println(session.query(new Select(Course.class).where(new ObjectCondition("getName()", Operation.Equal, "Math"))).get(0).getTeacher().getName());
//output: Majed