Subject: Dynamic proxy example without getters
From: "Denis St Flour"
Date: Tue, 17 Sep 2002 15:01:59 +1000
To: "OJB Users List (E-mail)"

I thought that could be of benefit to some beginners out there . Basically I wanted to use a dynamic proxy in a 1:1 relationship but wanted to have a generic interface where
I would not have to define getters for every object that use dynamic proxy. Here is how it goes:

Create an interface called IDynamicProxy.

package test.dsf;
public interface IOjbDynamicProxy {
  public IDataObject getDO() ;
}


IDataObject is an empty interface. It is used as a passed through mechanism. Every object implements IDataObject.

package test.dsf;
public interface IDataObject {
}




package test.dsf;
public class OjbPerson implements IDataObject {
  private Integer id;
  private String firstName;
  private String lastName;
  private Integer addressId;
  private IOjbDynamicProxy address;

  public OjbPerson() {
  }

  public OjbPerson(Integer id, String fn, String ln, Integer addressId) {
    this.id = id;
    firstName = fn;
    lastName = ln;
    this.addressId = addressId;
  }

  public IOjbDynamicProxy getAddress() {
    return address;

  }

}


package test.dsf;
import com.t2b.core.core.*;
import com.t2b.core.agents.state.*;
public class OjbAddress implements IOjbDynamicProxy, IDataObject {
  private Integer id;
  private String line1;
  private String line2;
  private String city;
  private String state;
  private String postCode;

  public OjbAddress() {
    super();
  }
  public OjbAddress(Integer id,String l1, String l2, String c, String s, String p) {
    id = id;
    line1 = l1;
    line2 = l2;
    city = c;
    state = s;
    postCode = p;
  }

  // this method will instantiate the OjbAddress when called
  public IDataObject getDO() {
    return this;
  }
}



Repository file is as follows:
<!-- Mapping of classes used in junit tests and tutorials starts here -->
<!-- Definitions for test.func.t2b.core.agents.persistence.ojb.Person -->
<class-descriptor
     class="test.dsf.OjbAddress"
  	  table="ADDRESS"
	  proxy="dynamic"
     >
      <field-descriptor id="1"
         name="id"
         column="ADDRESSID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
      />
      <field-descriptor id="2"
         name="line1"
         column="LINE1"
         jdbc-type="VARCHAR"
      />

      <field-descriptor id="3"
         name="line2"
         column="LINE2"
         jdbc-type="VARCHAR"
      />

      <field-descriptor id="4"
         name="city"
         column="CITY"
         jdbc-type="VARCHAR"
      />

      <field-descriptor id="5"
         name="state"
         column="STATE"
         jdbc-type="VARCHAR"
      />

	<field-descriptor id="6"
         name="postCode"
         column="POSTCODE"
         jdbc-type="VARCHAR"
      />

    </class-descriptor>



    <class-descriptor
   	  class="test.dsf.OjbPerson"
   	  table="PERSON"
   >
      <field-descriptor id="1"
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
      />
      <field-descriptor id="2"
         name="firstName"
         column="FIRSTNAME"
         jdbc-type="VARCHAR"
      />
      <field-descriptor id="3"
         name="lastName"
         column="LASTNAME"
         jdbc-type="VARCHAR"
      />

      <field-descriptor id="4"
         name="addressId"
         column="ADDRESSID"
         jdbc-type="INTEGER"
      />

    <reference-descriptor
         name="address"
          class-ref="test.dsf.OjbAddress"
    	  proxy = "dynamic"
      >
         <foreignkey field-id-ref="4"/>
      </reference-descriptor>
    </class-descriptor>


The TestCase:

 public void  testDynamicProxy() {
    // get odmg facade instance
    Implementation odmg = OJB.getInstance();
    Database db = odmg.newDatabase();
    OjbPerson p = null;
    OjbAddress a = null;
    try
    {
        db.open("repository.xml", Database.OPEN_READ_WRITE);
    }
    catch (ODMGException ex)
    {
        ex.printStackTrace();
    }

    Transaction tx = null;
    try {
      // 2. start transaction
      tx = odmg.newTransaction();
      tx.begin();
      String oqlQuery = "select result from test.dsf.OjbPerson where id = 1 " ;
      OQLQuery query = odmg.newOQLQuery();
      query.create(oqlQuery);
      DList result = (DList) query.execute();
      if (result.size() <= 0) {
        throw new Exception("No OjbPerson found");
      }
        p = (OjbPerson)result.get(0);
      tx.commit();
  }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    // get the referenced proxy	
    IOjbDynamicProxy proxy = p.getAddress();

    // instantiate the OjbAddress object
    OjbAddress oAddress = (OjbAddress) proxy.getDO();}
}




--
To unsubscribe, e-mail:   <mailto:ojb-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ojb-user-help@jakarta.apache.org>