Sorry I had forgotten the source... ============ JAVA code ============================= /* * Created on 28 Octobre 2004 */ package test.ojb;
import java.util.Vector; public class A { private int oid; private String name = null; private Vector elements = null; public A() { super(); } public A(String name) { super(); this.name = name; } public void add(B b){ if(elements == null) elements = new Vector(); elements.addElement(b); } public boolean isEqual(A a){ return this.oid == a.getOid(); } public int getOid() { return oid; } } package test.ojb; public class B { private int oid; private String value = null; private String filter = null; public B(){ super(); } public B(String filter, String value) { super(); this.filter = filter; this.value = value; } public boolean isEqual(B b){ return this.oid == b.getOid(); } public int getOid() { return oid; } public String getFilter() { return filter; } public String getValue() { return value; } } package test.ojb; import java.util.Collection; import org.apache.log4j.Category; import org.apache.log4j.Logger; import org.apache.ojb.broker.PersistenceBroker; import org.apache.ojb.broker.PersistenceBrokerFactory; import org.apache.ojb.broker.query.Criteria; import org.apache.ojb.broker.query.Query; import org.apache.ojb.broker.query.QueryByCriteria; import org.apache.ojb.broker.query.QueryFactory; /** * Projection test with OJB 1.0.0 */ public class ProjectionTest { private static Category logger = null; private PersistenceBroker broker = null; public ProjectionTest(){ if(logger == null) logger = Logger.getInstance(ProjectionTest.class) ; this.broker = PersistenceBrokerFactory.defaultPersistenceBroker(); } public static void main(String[] args) { ProjectionTest projectionTest = new ProjectionTest(); // uncomment the following line to initialize the database //projectionTest.initDB(); // load the collection with a SQL statement B[] sqlResult = projectionTest.getFilteredArray1("xxx", "yyy"); // load the collection with OJB projection B[] ojbResult = projectionTest.getFilteredArray2("xxx", "yyy"); if(sqlResult != null && ojbResult != null){ if(sqlResult.length == ojbResult.length){ logger.info("result size SQL == result size OJB "); // log obj results for (int i=0;i<ojbResult.length;i++) logger.info("B<" + ojbResult[i].getFilter() + ", " + ojbResult[i].getValue() + ">"); } else logger.info("result size SQL != result size OJB - Test KO."); } } public void initDB(){ // objects with a relationship 1:N A a1 = new A("xxx"); A a2 = new A("fff"); // populate the "elements" relationship for(int i=0; i<3;i++){ // a1 contains two categories of filter a1.add(new B("yyy", "aby" + i)); a1.add(new B("zzz", "abz" + i)); // a2 contains two categories of filter a2.add(new B("yyy", "bby" + i)); a2.add(new B("zzz", "bbz" + i)); } broker.beginTransaction(); // store all into the DB (auto-update=true) broker.store(a1); broker.store(a2); broker.commitTransaction(); // make sure the relationship is loaded from the DB broker.serviceObjectCache().clear(); } // example realized with a SQL (SQL92) statement public B[] getFilteredArray1(String name, String filter){ B[] ret = null; StringBuffer sqlStatement = new StringBuffer(); sqlStatement.append("select b.* from T_A a, T_B b where b.a_oid=A.oid and a.name='"); sqlStatement.append(name); sqlStatement.append("' and b.filter='"); sqlStatement.append(filter); sqlStatement.append("'"); Query query = QueryFactory.newQuery(B.class, sqlStatement.toString()); Collection col = broker.getCollectionByQuery(query); broker.serviceObjectCache().clear(); if(col != null){ ret = new B[col.size()]; col.toArray(ret); } return ret; } // example realize with a SQL (SQL92) statement public B[] getFilteredArray2(String name, String filter){ B[] ret = null; Criteria criteria = new Criteria(); // criteria relative to A criteria.addEqualTo("name", name); criteria.addEqualTo("elements.filter", filter); QueryByCriteria query = QueryFactory.newQuery(A.class, criteria); query.setObjectProjectionAttribute("elements"); Collection col = broker.getCollectionByQuery(query); broker.serviceObjectCache().clear(); if(col != null){ ret = new B[col.size()]; col.toArray(ret); } return ret; } } ================== SQL for Oracle 8i =========================== SPOOL projtest.log drop table T_A; create table T_A ( OID INTEGER NOT NULL, name VARCHAR2 ( 16 ) NULL, constraint PK_A PRIMARY KEY (OID) ); commit; drop table T_B; create table T_B ( OID INTEGER NOT NULL, filter VARCHAR2 ( 16 ) NULL, value VARCHAR2 ( 16 ) NULL, a_OID INTEGER NULL, constraint PK_B PRIMARY KEY (OID) ); commit; SPOOL OFF Exit 0 ; . / =================== repository.xml ================================== <?xml version='1.0' encoding='ISO-8859-1' ?> <descriptor-repository version="1.0" isolation-level="read-uncommitted" proxy-prefetching-limit="50"> <!-- Datasource for Proto1, this connection is used as the default --> <!-- Driver P6Spy : com.p6spy.engine.spy.P6SpyDriver --> <!-- Driver Oracle : oracle.jdbc.driver.OracleDriver --> <jdbc-connection-descriptor jcd-alias="sim" default-connection="true" platform="Oracle" jdbc-level="2.0" driver="com.p6spy.engine.spy.P6SpyDriver" protocol="jdbc" subprotocol="oracle:thin" dbalias="@xxxxxx" username="xxxxxxx" password="xxxxxxx" eager-release="false" batch-mode="false" useAutoCommit="1" ignoreAutoCommitExceptions="false" > <sequence-manager className="org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl"> <attribute attribute-name="autoNaming" attribute-value="true"/> </sequence-manager> </jdbc-connection-descriptor> <!-- Mapping --> <class-descriptor class="test.ojb.A" table="T_A" isolation-level="read-uncommitted" accept-locks="true" refresh="true"> <field-descriptor name="oid" column="oid" jdbc-type="INTEGER" primarykey="true" autoincrement="true" nullable="false"/> <field-descriptor name="name" column="name" jdbc-type="VARCHAR"/> <!-- elements relationship --> <collection-descriptor name="elements" element-class-ref="test.ojb.B" auto-update="true" auto-retrieve="true" auto-delete="true" proxy="true" > <inverse-foreignkey field-ref="aOid"/> </collection-descriptor> </class-descriptor> <class-descriptor class="test.ojb.B" table="T_B" isolation-level="read-uncommitted" accept-locks="true" refresh="true" > <field-descriptor name="oid" column="oid" jdbc-type="INTEGER" primarykey="true" autoincrement="true"/> <field-descriptor name="filter" column="filter" jdbc-type="VARCHAR"/> <field-descriptor name="value" column="value" jdbc-type="VARCHAR"/> <field-descriptor name="aOid" column="a_OID" jdbc-type="INTEGER" access="anonymous"/> </class-descriptor> </descriptor-repository> ========================== END source ============================== Gildas ----- Original Message ----- From: "Jakob Braeuchi" <[EMAIL PROTECTED]> To: "OJB Users List" <[EMAIL PROTECTED]> Sent: Wednesday, October 27, 2004 6:55 PM Subject: Re: collection loading and filtering hi gildas, could you please post the sql without and with projectionAttribute. i'd like to write a testcase and eventually document this feature. jakob --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]