[OJB-developers] Mapping Single Table Inheritance
Brought to you by:
thma
From: <Dal...@bd...> - 2002-02-07 22:26:34
|
I'm trying to map two classes to a single table, but I can't seem to get the mapping file correct. I have a basic inheritance test where B inherits from A. When I run the test case, the A classes read in fine, but I get a java.util.NoSuchElementException when I start reading in the B's. I'm using 0.7.325. What am I doing wrong? Here's what I have: A.java ------ package plprototype2.domain; public class A { protected String ojbConcreteClass; public int id; public String aString; public A() { ojbConcreteClass = A.class.getName(); } public A(int id, String objConcreteClass, String aString) { this.id = id; ojbConcreteClass = A.class.getName(); this.aString = aString; } public String getConcreteClass() { return ojbConcreteClass; } } B.java ------ package plprototype2.domain; public class B extends A { public String bString; public B() { ojbConcreteClass = B.class.getName(); } public B(int id, String objConcreteClass, String aString, String bString) { super(id, objConcreteClass, aString); ojbConcreteClass = B.class.getName(); this.bString = bString; } } SingleTableInheritanceTest.java ------------------------------- package plprototype2.tests; import junit.framework.Assert; import junit.framework.TestCase; import ojb.broker.PersistenceBroker; import ojb.broker.PersistenceBrokerException; import ojb.broker.PersistenceBrokerFactory; import ojb.broker.TransactionNotInProgressException; import ojb.broker.query.Criteria; import ojb.broker.query.Query; import ojb.broker.query.QueryByCriteria; import ojb.broker.util.Logger; import ojb.broker.util.LoggerFactory; import plprototype2.domain.A; import java.util.Iterator; public class SingleTableInheritanceTest extends TestCase { /** * 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 = SingleTableInheritanceTest.class; /** * Runs the suite in a junit.textui.TestRunner. */ public static void main(String[] args) { String[] arr = {CLASS.getName()}; junit.textui.TestRunner.main(arr); } protected PersistenceBroker broker; protected Logger logger; public SingleTableInheritanceTest(String name) { super(name); } /** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. */ protected void setUp() throws Exception { broker = PersistenceBrokerFactory.createPersistenceBroker(); logger = LoggerFactory.getLogger(this.getClass()); } /** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. */ protected void tearDown() throws Exception { try { broker.clearCache(); } catch (PersistenceBrokerException e) { logger.error("Exception during tearDown(): " + e.getMessage()); } } public void testSingleTableInheritance() { try { logger.info("Querying all A objects..."); // These should already be in the database (set up by mapping.sql). Criteria selectAll = null; Query q = new QueryByCriteria(A.class, selectAll); Iterator iterator = broker.getIteratorByQuery(q); while (iterator.hasNext()) { Object o = iterator.next(); A a = (A) o; logger.info("Checking " + a.getConcreteClass() + " vs. " + o.getClass().getName() + "."); Assert.assertEquals(a.getConcreteClass(), o.getClass().getName()); } logger.info("Inheritance query succeeded!"); } catch (Throwable t) { // As of ojb-0.7.325, isInTransaction() is not part of the // PersistenceBroker interface, so we have to check the hard way. try { broker.abortTransaction(); } catch (TransactionNotInProgressException e) { } logger.info("Inheritance query failed!"); t.printStackTrace(); Assert.assertTrue(false); } } } mapping.xml ----------- ... <driver stuff deleted> ... <ClassDescriptor id="9"> <class.name>plprototype2.domain.B</class.name> <table.name>AB_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>ojbConcreteClass</field.name> <column.name>CLASS_NAME</column.name> <jdbc_type>VARCHAR</jdbc_type> </FieldDescriptor> <FieldDescriptor id="3"> <field.name>aString</field.name> <column.name>A_STRING</column.name> <jdbc_type>VARCHAR</jdbc_type> </FieldDescriptor> <FieldDescriptor id="4"> <field.name>bString</field.name> <column.name>B_STRING</column.name> <jdbc_type>VARCHAR</jdbc_type> </FieldDescriptor> </ClassDescriptor> <ClassDescriptor id="10"> <class.name>plprototype2.domain.A</class.name> <class.extent>plprototype2.domain.B</class.extent> <table.name>AB_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>ojbConcreteClass</field.name> <column.name>CLASS_NAME</column.name> <jdbc_type>VARCHAR</jdbc_type> </FieldDescriptor> <FieldDescriptor id="3"> <field.name>aString</field.name> <column.name>A_STRING</column.name> <jdbc_type>VARCHAR</jdbc_type> </FieldDescriptor> </ClassDescriptor> ... schema.sql ---------- DROP TABLE AB_TABLE; CREATE TABLE AB_TABLE ( ID INT NOT NULL PRIMARY KEY, CLASS_NAME VARCHAR(128), A_STRING VARCHAR(60), B_STRING VARCHAR(60) ); INSERT INTO AB_TABLE VALUES (1,'plprototype2.domain.A','A1',NULL); INSERT INTO AB_TABLE VALUES (2,'plprototype2.domain.A','A2',NULL); INSERT INTO AB_TABLE VALUES (3,'plprototype2.domain.A','A3',NULL); INSERT INTO AB_TABLE VALUES (4,'plprototype2.domain.B','B4','B4'); INSERT INTO AB_TABLE VALUES (5,'plprototype2.domain.B','B5','B5'); output ------ .[BOOT] INFO: OJB.properties: file:/C:/Personal/plprototype2/src/OJB.properties 0 INFO [main] tests.SingleTableInheritanceTest - Querying all A objects... 671 WARN [main] DEFAULT - Please define a public constructor for class plprototype2.domain.A with the following signature: (int, java.lang.String, java.lang.String). It must initialize the classes persistent attributes. This is recommended to increase performance but it's not mandatory! 681 INFO [main] tests.SingleTableInheritanceTest - Checking plprototype2.domain.A vs. plprototype2.domain.A. 681 INFO [main] tests.SingleTableInheritanceTest - Checking plprototype2.domain.A vs. plprototype2.domain.A. 691 INFO [main] tests.SingleTableInheritanceTest - Checking plprototype2.domain.A vs. plprototype2.domain.A. 691 WARN [main] DEFAULT - Please define a public constructor for class plprototype2.domain.B with the following signature: (int, java.lang.String, java.lang.String, java.lang.String). It must initialize the classes persistent attributes. This is recommended to increase performance but it's not mandatory! 691 ERROR [main] accesslayer.RsIterator - 3 701 INFO [main] tests.SingleTableInheritanceTest - Inheritance query failed! java.util.NoSuchElementException at ojb.broker.accesslayer.RsIterator.next(RsIterator.java:151) at plprototype2.tests.SingleTableInheritanceTest.testSingleTableInheritance(SingleTableInheritanceTest.java:81) at java.lang.reflect.Method.invoke(Native Method) at junit.framework.TestCase.runTest(TestCase.java:166) at junit.framework.TestCase.runBare(TestCase.java:140) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:131) at junit.framework.TestSuite.runTest(TestSuite.java:173) at junit.framework.TestSuite.run(TestSuite.java:168) at junit.textui.TestRunner.doRun(TestRunner.java:74) at junit.textui.TestRunner.start(TestRunner.java:234) at junit.textui.TestRunner.main(TestRunner.java:112) at plprototype2.tests.SingleTableInheritanceTest.main(SingleTableInheritanceTest.java:39) F Time: 1.953 There was 1 failure: 1) testSingleTableInheritance(plprototype2.tests.SingleTableInheritanceTest)junit.framework.AssertionFailedError at plprototype2.tests.SingleTableInheritanceTest.testSingleTableInheritance(SingleTableInheritanceTest.java:99) at plprototype2.tests.SingleTableInheritanceTest.main(SingleTableInheritanceTest.java:39) FAILURES!!! Tests run: 1, Failures: 1, Errors: 0 |