Object-Relational Mapping in Java with SimpleORM
by Martin Snyder and Ted O'Connor

Listing One 

public static class Folder extends SRecordInstance
{
  static SRecordMeta s_meta = new SRecordMeta(Folder.class, "FOLDER");

  static SFieldInteger ID = new SFieldInteger(s_meta,"ID",new SPropertyValue[]
{ SSimpleORMProperties.SFD_PRIMARY_KEY, 
                                  SSimpleORMProperties.SFD_GENERATED_KEY });
  static SFieldReference PARENT = new SFieldReference(s_meta,s_meta,"PARENT");
  static SFieldString NAME = new SFieldString(s_meta, "NAME", 64,
                                  SSimpleORMProperties.SFD_MANDATORY);
  public SRecordMeta getMeta() { return s_meta; }
}
public static class Property extends SrecordInstance
{
  static SRecordMeta s_meta = new SRecordMeta(Property.class, "PROPERTY");
  static SFieldReference PARENT = 
     new SFieldReference(s_meta, Folder.s_meta, "PARENT", new SPropertyValue[]
     { SSimpleORMProperties.SFD_PRIMARY_KEY, 
          SSimpleORMProperties.SFD_MANDATORY });
  static SFieldString NAME = new SFieldString(s_meta, "NAME", 64,
          new SpropertyValue[]
         { SSimpleORMProperties.SFD_PRIMARY_KEY, 
           SSimpleORMProperties.SFD_MANDATORY });
  static SFieldObject VALUE = new SFieldObject(s_meta, "VALUE");
  public SRecordMeta getMeta() { return s_meta; }
}


Listing Two 

Connection conn = DriverManager.getConnection("jdbcURL", "user", "pass");
SConnection.attach(conn, "Connection Name");
SConnection.begin();
 ...
SConnection.commit();
SConnection.detachAndClose();

Listing Three

Folder folder1 = (Folder)Folder.s_meta.findOrCreate(new Integer(5));
Folder1.assertNotNewRow();
SRecordInstance folder2 = Folder.s_meta.createWithGeneratedKey();
String name = folder1.getString(Folder.NAME);
Folder parent = (Folder)folder2.getReference(Folder.PARENT);

Object idObject = folder2.getObject(Folder.ID);
String idString = folder2.getString(Folder.ID);
Int idInt = folder2.getInt(Folder.ID);

folder1.deleteRecord();


Listing Four 

public static class Folder extends SRecordInstance
{
  ...
  public void deleteRecord()
  {
    // Delete all subfolders
    SResultSet subFolders = s_meta.newQuery().eq(PARENT, this).execute();
    while (subFolders.hasNext())
      subFolders.getRecord().deleteRecord();

    // Delete all properties of this folder
    SResultSet props = 
            Property.s_meta.newQuery().eq(Property.PARENT, this).execute();
    while (props .hasNext())
      props.getRecord().deleteRecord();
    // Invoke the base implementation
    super.deleteRecord();
  }


Listing Five 

SQuery query = Folder.s_meta.newQuery().isNull(Folder.PARENT).
                                                      ascending(Folder.NAME);
SResultSet rs = query.execute();
while (rs.hasNext)
{
    Folder folder = (Folder)rs.getRecord();
    ...
}


Listing SIx 

SQuery query = Folder.s_meta.newQuery()
                .isNull(Folder.PARENT)
                .and()
                .eq(Folder.NAME, "Test");
SResultSet rs = query.execute();
Folder folder = (Folder)rs.getOnlyRecord();


Listing Seven

public static class Folder extends SRecordInstance
{
  ...
  public static final String DELIMETER = "/";
  public void validateField(SFieldMeta field, Object newValue)
  {
    if (NAME == field)
    {
      if (null == newValue ||
          "".equals(newValue) ||
         (newValue.toString().indexOf(DELIMETER) >= 0))
      {
        throw new SValidationException("Folder name cannot be blank or 
                    contain " + "the DELIMETER character: " + DELIMETER);
      }
    }
  }
}





3


