simpleorm.quickstart

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Natural Language Processing
51.Net
52.Parser
53.PDF
54.Portal
55.Profiler
56.Project Management
57.Report
58.RSS RDF
59.Rule Engine
60.Science
61.Scripting
62.Search Engine
63.Security
64.Sevlet Container
65.Source Control
66.Swing Library
67.Template Engine
68.Test Coverage
69.Testing
70.UML
71.Web Crawler
72.Web Framework
73.Web Mail
74.Web Server
75.Web Services
76.Web Services apache cxf 2.2.6
77.Web Services AXIS2
78.Wiki Engine
79.Workflow Engines
80.XML
81.XML UI
Java Source Code / Java Documentation  » Database ORM » SimpleORM » simpleorm.quickstart 
simpleorm.quickstart
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!

Java Source File NameTypeComment
AParam.javaClass
ColumnGenerate.javaClass
DefaultFormatter.javaClass This is the standard name conversion used by SimpleORMGenerator.
ForeignKeyGenerate.javaClass
INiceNameFormatter.javaInterface Interface to convert the database table or column name to your names.
SimpleORMGenerate must be created with an object that instanciates this interface.

Typically you should simple algrothem to return a nice name (like DefaultFormatter), but the code could be complex as you feel like (like SimpleORMNiceNameFormatter).

SimpleORMGenerator.javaClass This class generates the SimpleORM mapping files for a (in this example) Sybase schema.
SimpleORMNiceNameFormatter.javaClass
TableGenerate.javaClass
w_w__w.___j_a__v_a2_s._c___o___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.