I am working with Java 1.7, IDE is Eclipse Indigo.
I have a base class which has id
as an int. It also has a boolean variable onlyIdInitialized
; I am setting it to true when an object is created with id
.
public abstract class BaseBO {
protected Boolean onlyIdInitialized = null;
private int id;
public BaseBO(int id) {
this.id = id;
onlyIdInitialized = true;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Boolean isOnlyIdInitialized() {
return onlyIdInitialized;
}
}
Now for each class which inherits from the above class; I want each of their field's setter method to set boolean variable onlyIdInitialized
to false i.e. onlyIdInitialized = false;
I could do that in a crude and raw way for example:
public class Location extends BaseBO {
private String description;
private boolean active;
public Location(int id) {
super(id);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
this.onlyIdInitialized = false;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active= active;
this.onlyIdInitialized = false;
}
}
But it will be a nightmare to maintain and looks not good. Is there an elegant solution to this problem? Is there a design pattern which solves this problem?
UPDATE:
@ThomasJunk
I have a class Employee
which has a reference to class Location
.
public class Employee extends BaseBO {
private String employeeNumber;
private Location location;
public Employee(int id) {
super(id);
}
public String getEmployeeNumber() { return employeeNumber; }
public void setEmployeeNumber(String employeeNumber) { this.employeeNumber = employeeNumber; }
public Location getLocation() { return location; }
public void setLocation(Location location) { this.location = location; }
}
I have already mentioned the Location
class above which has 2 fields description
and active
in addition to id
field.
When Employee
object is build from retrieve of 1 row of employee table, I only have the id
of the concerned location; I dont have the description
and active
of the concerned location. If the calling program also want to access the description
and active
of the concerned location then I want a way to know that if the description
and active
of the concerned location are already retrieved: if already retrieved then just give their access; if not already retrieved then retrieved them first and then give their access.
UPDATE 2:
@ThomasJunk @Laiv
I am not sure my question is a lazy fetch of an object (if I understand it correctly). In my case the location
object would already exist but it only has id
field populated. For example in my EmployeeDAO I would have this method (psuedo-code):
public Employee getEmployeeById(int employeeId) throws SQLException {
ResultSet resultSet = null;
//...
String employeeNumber = resultSet.getInt(2, 'employee_number');
int locationId = resultSet.getInt(3, 'location_id');
Location location = new Location(locationId);
Employee employee = new Employee(employeeId);
employee.setEmployeeNumber(employeeNumber);
employee.setLocation(location);
return employee;
}
But the location object does not have other fields description
and active
populated so is it not correct to say that its lazy fetch of other fields of an object?
BaseBO
, the same code is included in the setter method. Thats why I think it would be nightmare to maintain.id
is populated or other fields are also populated of each subclass. I forgot to add a method inBaseBO
which I have added now:public Boolean isOnlyIdInitialized() { return onlyIdInitialized; }