R
Rizwan
For each table in my database, I have created a seperate class (java-beans
style) whose attributes matches to the columns of its table. If its a
foreign key then the datatype is actually another class.
CREATE TABLE LOCATION (
location_id int NOT NULL PRIMARY KEY,
location_name varchar(50) NOT NULL,
location_flag int NULL )
CREATE TABLE EMPLOYEE (
employee_id int NOT NULL PRIMARY KEY,
first_name varchar(25) NOT NULL,
middle_name varchar(25) NULL,
last_name varchar(25) NOT NULL,
location_id int NOT NULL REFERENCES LOCATION(location_id) )
Then my classes are :
public class Location implements java.io.Serializable {
private int locationId;
private String locationName;
private Integer locationFlag;
public Location() {}
// Setters and Getters
...
}
public class Employee implements java.io.Serializable {
private int employeeId;
private String firstName;
private String middleName;
private String lastName;
private Location location;
public Employee() {}
// Setters and Getters
...
}
The reason for creating these classes is that for each data request, the
data can be passed in the shape of these classes. For that the logic I have
in my mind is :
* data will be first retrieved in a resultset
* the concerned class's object is instantiated
* its attributes are populated through its setters
* the object is passed to the data request module
Now I have some questions:
1) Taking Employee example sometimes the data request is only concerned
about Employee data (EMPLOYEE table only) and some time it is concerned
about the corresponding Location data as well (EMPLOYEE and LOCATION table).
Should I pass Employee class in both cases?
Or Should I create another class which is same as Employee class but with no
Location class reference and pass this new class?
public class EmployeeRaw implements java.io.Serializable {
private int employeeId;
private String firstName;
private String middleName;
private String lastName;
private int locationId;
public Employee() {}
// Setters and Getters
...
}
2) Sometimes the data request cannot be represented in a class which
represents a table. Its a combination of more than one class (here class
means one which strictly represents a table). What is the solution in this
case?
3) In the above scenario Employee class has Location class which has no
other class. I will call this as 1 level deep. But there are some scenarios
where a class has another class which in turn also has some class. Its like
more than 1 level deep. In this scenario what is the recommended practice
regarding retrieval?
Thanks
style) whose attributes matches to the columns of its table. If its a
foreign key then the datatype is actually another class.
CREATE TABLE LOCATION (
location_id int NOT NULL PRIMARY KEY,
location_name varchar(50) NOT NULL,
location_flag int NULL )
CREATE TABLE EMPLOYEE (
employee_id int NOT NULL PRIMARY KEY,
first_name varchar(25) NOT NULL,
middle_name varchar(25) NULL,
last_name varchar(25) NOT NULL,
location_id int NOT NULL REFERENCES LOCATION(location_id) )
Then my classes are :
public class Location implements java.io.Serializable {
private int locationId;
private String locationName;
private Integer locationFlag;
public Location() {}
// Setters and Getters
...
}
public class Employee implements java.io.Serializable {
private int employeeId;
private String firstName;
private String middleName;
private String lastName;
private Location location;
public Employee() {}
// Setters and Getters
...
}
The reason for creating these classes is that for each data request, the
data can be passed in the shape of these classes. For that the logic I have
in my mind is :
* data will be first retrieved in a resultset
* the concerned class's object is instantiated
* its attributes are populated through its setters
* the object is passed to the data request module
Now I have some questions:
1) Taking Employee example sometimes the data request is only concerned
about Employee data (EMPLOYEE table only) and some time it is concerned
about the corresponding Location data as well (EMPLOYEE and LOCATION table).
Should I pass Employee class in both cases?
Or Should I create another class which is same as Employee class but with no
Location class reference and pass this new class?
public class EmployeeRaw implements java.io.Serializable {
private int employeeId;
private String firstName;
private String middleName;
private String lastName;
private int locationId;
public Employee() {}
// Setters and Getters
...
}
2) Sometimes the data request cannot be represented in a class which
represents a table. Its a combination of more than one class (here class
means one which strictly represents a table). What is the solution in this
case?
3) In the above scenario Employee class has Location class which has no
other class. I will call this as 1 level deep. But there are some scenarios
where a class has another class which in turn also has some class. Its like
more than 1 level deep. In this scenario what is the recommended practice
regarding retrieval?
Thanks