[OJB-developers] mapping inheritance hierarchies question
Brought to you by:
thma
From: Matt G. <mat...@ss...> - 2002-01-03 18:00:32
|
Hello again, I've been trying to get the "mapping all classes on the same table" scenario working. I think there's a bug in the code that instantiates these objects. I originally posted about problems with this in December last year - I should have followed it up sooner :(. The easiest way to hilight the problem is by extending the PersistenceBrokerTest.java unit tests - at the end of the message is a modified version of the testMappingToOneTable() test. AFAICT, the test now adds 2 A's and 1 B, I haven't found any other tests which add any more. I would therefore expect the table to hold 2 A's and 1 B which should mean that a search for AB's, A's and B's should return 3, 2 and 1 objects respectively. You actually get 6, 3 and 3 objects! Hopefully, I've understood how to use OJB in this way. If not, my apologies for saying there's a bug ;) Hopefully, one day (if I ever get to use OJB properly), I'll be able to send patches in instead. Cheers, Matt ---------------------------------------------------------------------- /** * performs a test of the inheritance mapping to one table. */ public void testMappingToOneTable() { try { A a1 = new A(); Identity iA1 = new Identity(a1); A a2 = new A(); Identity iA2 = new Identity(a2); B b1 = new B(); Identity iB2 = new Identity(b1); broker.store(a1); broker.store(a2); broker.store(b1); AB ab = null; Criteria crit = new Criteria(); crit.addEqualTo("id", new Integer(a1.id)); Query q = QueryFactory.newQuery(AB.class, crit); ab = (AB) broker.getObjectByQuery(q); assertEquals(A.class.getName(), ab.getConcreteClass()); assertEquals(A.class, ab.getClass()); crit = new Criteria(); crit.addEqualTo("id", new Integer(b1.id)); q = QueryFactory.newQuery(AB.class, crit); ab = (AB) broker.getObjectByQuery(q); assertEquals(B.class.getName(), ab.getConcreteClass()); assertEquals(B.class, ab.getClass()); Collection abs = null; // Test retrieval of collections q = QueryFactory.newQuery(AB.class, null); abs = broker.getCollectionByQuery(q); //System.out.println("AB's = " + abs.size()); assertEquals(abs.size(), 3); q = QueryFactory.newQuery(A.class, null); abs = broker.getCollectionByQuery(q); //System.out.println("A's = " + abs.size()); assertEquals(abs.size(), 2); q = QueryFactory.newQuery(B.class, null); abs = broker.getCollectionByQuery(q); //System.out.println("B's = " + abs.size()); assertEquals(abs.size(), 1); } catch (Throwable t) { fail(t.getMessage()); } } ---------------------------------------------------------------------- --- Matt Goodall Analyst Programmer, Strategic Systems Solutions e: mat...@ss..., t: +44 113 3892645 |