SimpleORMGenerator
SimpleORMGenerator provides a simple way of generating the
SimpleORM class files for an existing database. Since
SimpleORMGenerator uses the meta data extracted from the database the
class files produced should exactly match the database's schema.
Class files produced
Every table produces two class files, a base class that contains
the field definitions and a public class that extends the base class.
Example given table EMPLOYEE
file Employee_base.java
class Employee_base {
//all table fields are defined here
}
File Employee.java
public class Employee extends Employee_base{
//place your business rules in this class.
}
The Employee_base class is defined as package private, which means
that it can't be seen from outside of the package. All interaction to
the database table has to pass through Employee and therefore through
any business rules that you add.
The base class is regenerated every time SimpleORMGenerator is run,
while the public class is only generated if it does not exit. This
allows you to edit the pubic class to your hearts content and still
be easily update your classes with any changes made to the database.
Getters and Setters
SimpleORMGenerator produces getters and setters for every field in
the table. As these methods are creates automatically there is no
method maintenance is required. Having getters and setters makes it
easy to insure that the correct data type is passed to a field at
compile time instead of runtime.
Example
public String get_NAME(){
return getString(NAME);
}
public void set_NAME( String value){
setString( NAME,value);
}
Foreign references key fields.
SimpleORMGenerator does generate code that uses
SFieldReference fields to represent
foreign keys. This is because SfieldReference fields represent links
between tables in a conceptual model of the database. Trying to
re-engineer these conceptual links from a physical model has not proved
very successful. The mapping is complex and it appears that SimpleORM
may contain a number of bugs in this area.
To get around this problem SimpleORMGenerator uses a simple
solution - getters and setters for reference fields.
Example (simplified)
public Department get_DEPARTMENT(){
return Department.findOrCreate(get_DEPT_ID());
}
public void set_DEPARTMENT( Department value){
set_DEPT_ID( value.get_DEPT_ID());
}
One side effect of this approach is that if the classes are to
generate database tables, the foreign keys between tables will be
lost. The pay off is that there is no type casting required when getting a record!
Find or Create methods.
SimpleORMGenerator creates a set of static FindOrCreate methods for
the classes. These are far simple to use that provided by the meta
object as the parameters are implicitly defined.
Example
public static Employee findOrCreate( String _EMPEE_ID ){
return (Employee) meta.findOrCreate( new Object[]{_EMPEE_ID});
}
If a foreign key appears in the primary keys of the table then
additional FindOrCreate method will be generated with the foreign key
as one of its parameters
Example
public static PaySlip findOrCreate( Employee _ref, long _YEAR, long _PERIOD){
return findOrCreate( _ref.get_EMPEE_ID(), _YEAR, _PERIOD);
}
Naming of fields
SimpleORMGenerator uses a version of adapter pattern to allow uses
to implement their own field name strategy. When running
SimpleORMGenerator a class that implements interface
simpleorm.quickstart.IniceNameFormatter. This class can then
implement the required field naming algorithm. See
simpleorm.quickstart.SimpleORMNiceNameFormatter
for an example.
Running SimpleORMGenerator.
The required system properties must be set. Create an instance of
SimpleORMGenerator and then call the execute() method. Or do what I do
and simply edit the code in the main() method!
|