We have encountered another bug in 1.0.3. We are using ODMG, though I
suspect this one is a PB bug. It also looks like it's related to the
problem reported last week by Bartłomiej Knabel ("[PB] two relations
between two classes").
Summary: I have a class "Parent" which has two separate m:n relationships
to the same class "Child". All entries for both relationships are always
written to the indirection table for the first one that appears in
repository.xml - nothing is ever written to the second indirection table.
If I put entries in both indirection tables by hand, then the object is
materialized correctly.
Here's a test case:
================ OjbTest.java ================
package ojbtest;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.ojb.odmg.OJB;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.OQLQuery;
import org.odmg.Transaction;
public class OjbTest extends TestCase {
private Implementation fImplementation = null;
private Database fDatabase = null;
public OjbTest() { }
public void testTwoMToNOfSameClass()
throws Exception
{
Transaction tx = fImplementation.newTransaction();
tx.begin();
Parent parent = new Parent(1);
fDatabase.makePersistent(parent);
Child child = new Child(20);
fDatabase.makePersistent(child);
List list = new ArrayList();
list.add(child);
parent.setChildren(list);
child = new Child(30);
fDatabase.makePersistent(child);
list = new ArrayList();
list.add(child);
parent.setOtherChildren(list);
tx.commit();
// Make sure that DO's are built correctly
Assert.assertEquals(1, parent.getChildren().size());
Assert.assertEquals(20, ((Child)
parent.getChildren().get(0)).getId());
Assert.assertEquals(1, parent.getOtherChildren().size());
Assert.assertEquals(30, ((Child)
parent.getOtherChildren().get(0)).getId());
// Retrieve the parent and see what we get
tx = fImplementation.newTransaction();
tx.begin();
OQLQuery query = fImplementation.newOQLQuery();
query.create("select x from ojbtest.Parent where id = 1");
parent = (Parent) ((List) query.execute()).get(0);
Assert.assertEquals(1, parent.getChildren().size());
Assert.assertEquals(20, ((Child)
parent.getChildren().get(0)).getId());
Assert.assertEquals(1, parent.getOtherChildren().size());
Assert.assertEquals(30, ((Child)
parent.getOtherChildren().get(0)).getId());
tx.commit();
}
protected void setUp()
throws Exception
{
super.setUp();
if (fImplementation == null) {
fImplementation = OJB.getInstance();
fDatabase = fImplementation.newDatabase();
fDatabase.open("tails", fDatabase.OPEN_READ_WRITE);
}
}
}
================ Parent.java ================
package ojbtest;
import java.util.List;
public class Parent {
private int fId;
private List fChildren;
private List fOtherChildren;
public Parent() { }
public Parent(int id) { fId = id; }
public void setId(int id) { fId = id; }
public int getId() { return fId; }
public void setChildren(List list) { fChildren = list; }
public List getChildren() { return fChildren; }
public void setOtherChildren(List list) { fOtherChildren = list; }
public List getOtherChildren() { return fOtherChildren; }
}
================ Child.java ================
package ojbtest;
import java.util.List;
public class Child {
private int fId;
public Child() { }
public Child(int id) { fId = id; }
public void setId(int id) { fId = id; }
public int getId() { return fId; }
}
================ repository_user.xml ================
<!-- - - - - - - Parent - - - - - - -->
<class-descriptor
class="ojbtest.Parent"
table="PARENT" >
<field-descriptor
name="id"
column="ID"
jdbc-type="INTEGER"
primarykey="true"
/>
<object-cache
class="org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl" />
<collection-descriptor
name="children"
element-class-ref="ojbtest.Child"
indirection-table="PARENT_CHILD"
auto-delete="none"
auto-update="none" >
<fk-pointing-to-this-class column="PARENT_ID" />
<fk-pointing-to-element-class column="CHILD_ID" />
</collection-descriptor>
<collection-descriptor
name="otherChildren"
element-class-ref="ojbtest.Child"
indirection-table="OTHER_PARENT_CHILD"
auto-delete="none"
auto-update="none" >
<fk-pointing-to-this-class column="PARENT_ID" />
<fk-pointing-to-element-class column="CHILD_ID" />
</collection-descriptor>
</class-descriptor>
<!-- - - - - - - Child - - - - - - -->
<class-descriptor
class="ojbtest.Child"
table="CHILD" >
<object-cache
class="org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl" />
<field-descriptor
name="id"
column="ID"
jdbc-type="INTEGER"
primarykey="true"
/>
</class-descriptor>
================ schema.sql ================
create table PARENT ( ID INTEGER not null, primary key(ID) );
create table CHILD ( ID INTEGER not null, primary key(ID) );
create table PARENT_CHILD (PARENT_ID INTEGER, CHILD_ID INTEGER);
create table OTHER_PARENT_CHILD (PARENT_ID INTEGER, CHILD_ID INTEGER);
================ end ================
thanks,
-steve
Steve Clark
ECOS Development Group
[EMAIL PROTECTED]
(970)226-9291
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]