AW: [OJB-developers] Base class queries on inheritance mapped to 2 tables
Brought to you by:
thma
From: Mahler T. <tho...@it...> - 2002-02-12 09:59:20
|
Hi Dale, > Is it possible to query for all instances of a base class if > its descendents are > mapped to another table using the PersistenceBroker API? I'm > using inheritance > method #2 - two classes/two tables, each with all the fields > from the base class > duplicated and the extents statement in the mapping > definition for the base > class specifying its descendents. When I query for all > instances of the base, > however, I never get any of the descendents. If this > functionality is supposed > to work in OJB, what am I doing wrong? > This scenario is supported. There are several testcases in the package test.ojb.broker testing this behaviour. I the latest release (0.7.343) there have been some fixes to inheritance related stuff. Please check if you still have problems with this new release. HTH, Thomas > > Base.java > --------------- > package plprototype2.domain; > > public class Base { > > public int id; > public String aString; > > public Base() { > } > > public Base(int id, String aString) { > this.id = id; > this.aString = aString; > } > > } > > > Descendent.java > --------------- > package plprototype2.domain; > > public class Descendent extends Base { > public String bString; > > public Descendent() { > } > > public Descendent(int id, String aString, String bString) { > super(id, aString); > this.bString = bString; > } > } > > > schema.sql > --------------- > DROP TABLE BASE_TABLE; > CREATE TABLE BASE_TABLE > ( ID INT NOT NULL PRIMARY KEY, > A_STRING VARCHAR(60) > ); > > DROP TABLE DESCENDENT_TABLE; > CREATE TABLE DESCENDENT_TABLE > ( ID INT NOT NULL PRIMARY KEY, > A_STRING VARCHAR(60), > B_STRING VARCHAR(60) > ); > > INSERT INTO BASE_TABLE VALUES (1,'Base 1'); > INSERT INTO BASE_TABLE VALUES (2,'Base 2'); > > INSERT INTO DESCENDENT_TABLE VALUES (1,'Descendent 1','1'); > INSERT INTO DESCENDENT_TABLE VALUES (2,'Descendent 2','2'); > > > repositor.xml > --------------- > ...<stuff clipped>... > > <ClassDescriptor id="26"> > <class.name>plprototype2.domain.Base</class.name> > <class.extent>plprototype2.domain.Descendent</class.extent> > <table.name>BASE_TABLE</table.name> > <FieldDescriptor id="1"> > <field.name>id</field.name> > <column.name>ID</column.name> > <jdbc_type>INTEGER</jdbc_type> > <PrimaryKey>true</PrimaryKey> > <autoincrement>true</autoincrement> > </FieldDescriptor> > <FieldDescriptor id="2"> > <field.name>aString</field.name> > <column.name>A_STRING</column.name> > <jdbc_type>VARCHAR</jdbc_type> > </FieldDescriptor> > </ClassDescriptor> > > <ClassDescriptor id="25"> > <class.name>plprototype2.domain.Descendent</class.name> > <table.name>DESCENDENT_TABLE</table.name> > <FieldDescriptor id="1"> > <field.name>id</field.name> > <column.name>ID</column.name> > <jdbc_type>INTEGER</jdbc_type> > <PrimaryKey>true</PrimaryKey> > <autoincrement>true</autoincrement> > </FieldDescriptor> > <FieldDescriptor id="2"> > <field.name>aString</field.name> > <column.name>A_STRING</column.name> > <jdbc_type>VARCHAR</jdbc_type> > </FieldDescriptor> > <FieldDescriptor id="3"> > <field.name>bString</field.name> > <column.name>B_STRING</column.name> > <jdbc_type>VARCHAR</jdbc_type> > </FieldDescriptor> > </ClassDescriptor> > > ...<stuff clipped>... > > > PerClassTableInheritanceTest.java > --------------- > package plprototype2.tests; > > import ojb.broker.query.Criteria; > import ojb.broker.query.Query; > import ojb.broker.query.QueryByExample; > import ojb.broker.query.QueryByCriteria; > import plprototype2.domain.Base; > > import java.util.Collection; > > import junit.framework.Assert; > > public class PerClassTableInheritanceTest extends OJBTestCase { > > /** > * Static reference to .class. Java does not provide any > way to obtain the > * Class object from static method without naming it. > */ > private static Class CLASS = PerClassTableInheritanceTest.class; > > /** > * Runs the suite in a junit.textui.TestRunner. > */ > public static void main(String[] args) { > String[] arr = {CLASS.getName()}; > junit.textui.TestRunner.main(arr); > } > > public PerClassTableInheritanceTest(String name) { > super(name); > } > > /** > * Query all objects of Type "Base". You should get 2 from > BASE_TABLE and > * 2 from DESCENDENT_TABLE. > */ > public void testInheritance() { > Criteria selectAll = null; > Query q = new QueryByCriteria(Base.class, selectAll); > Collection c = broker.getCollectionByQuery(q); > logger.info("Base class query returned "+c.size()+" objects."); > Assert.assertEquals("Incorrect number of Base objects > returned!", 4, > c.size()); > } > } > > > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > |