Hi Brad,
good news! Seems to work.
I checked in a new test case show how to use PB-api to store/retrieve/delete object hierarchies via m:n relation with different collection-descriptor auto-xxx settings.
Main difference to your test is that I use two table (one for tree object, the other as indirection table). But it should also be possible to map this stuff in one table (not recommended IMO, because it will mix different types of rows).
The test is called "M2NGraphTest.java" (see test-suite, [db-ojb]/src/test) and is a little different from yours, but following the example should make work your test too.
http://cvs.apache.org/viewcvs.cgi/db-ojb/src/test/org/apache/ojb/broker/M2NGraphTest.java?rev=1.1&view=markup
The metadata mapping can be found in src/test/org/apache/ojb repository_junit_reference.xml
By the way, your wrapper methods addEntity, linkEntity use different PB instances, thus two connections will be used. If linkEntity fails the changes made in addEntity will not be rollback. And you don't close the used PB instance.
But maybe you override this methods in your wrapper too, so my notice is wrong ;-)
regards, Armin
Bradford Pielech wrote:
yeah, those are basically wrappers to underlying broker methods.
The addEntity method is a wrapper for the following method that accesses the persistence broker:
----------------------------
public boolean addEntity(SimpleDagNode nodeA) throws Exception {
PersistenceBroker pbroker = PersistenceBrokerFactory.defaultPersistenceBroker();
pbroker.beginTransaction();
pbroker.store(nodeA, modificationType);
pbroker.commitTransaction();
return true;
----------------------------
and broker.linkEntity is implemented as follows:
----------------------------
public void linkEntity(SimpleDagNode parent, SimpleDagNode child) throws Exception {
if(!parent.getChildren().contains(child)){
parent.addChild(child);
}
PersistenceBroker pbroker = PersistenceBrokerFactory.defaultPersistenceBroker();
pbroker.beginTransaction();
pbroker.serviceBrokerHelper().link(o, false);
pbroker.commitTransaction();
}
----------------------------
At 03:05 PM 6/23/2004 +0200, you wrote:
seems you use an user specific PersistenceBroker implementation, can you post methods
broker.addEntity(nodeA); broker.linkEntity(nodeA, nodeB);
too.
regards, Armin
Bradford Pielech wrote:
Oops, just realized there was a logic bug in the junit test that makes the code incorrect because I had to quickly rewrite the test to remove unneeded subclasses and such. Here is the correct version:
public void testAddNewChild() throws Exception {
SimpleDAGNode nodeA = new nodeANode();
SimpleDAGNode nodeB = new nodeBNode();
//store nodeA first
broker.addEntity(nodeA);
//then store the nodeB
broker.addEntity(nodeB);
//make A the parent of B because they are not linked in the code automatically
broker.linkEntity(nodeA, nodeB);
SimpleDAGNode retrievednodeB = broker.getEntity(nodeB.getID());
assertNotNull("Verifying the nodeB was retrieved", retrievednodeB);
SimpleDAGNode retrievednodeA = (SimpleDAGNode) retrievednodeB.getParentAt(0);
assertEquals("verify the nodeA was stored", nodeA.getID(),
retrievednodeA.getEboId());
assertEquals("verify the retrieved nodeA has 1 child (the nodeB)", 1,
retrievednodeA.getChildCount());
assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)", 1,
retrievednodeB.getParentCount());
assertEquals(
"verify, using hashcode, that the nodeB's parent is the nodeA",
retrievednodeA.hashCode(),
retrievednodeB.getParentAt(0).hashCode());
for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext(); ) {
Object o = i.next();
//this next test fails because the child is null
assertNotNull("Verifying nodeA's children are not null", o);
assertEquals(
"verify, using hashcode, that the nodeAs child is the nodeB",
retrievednodeB.hashCode(), o.hashCode());
}
}
At 08:18 AM 6/23/2004 -0400, you wrote:
Armin:
Sure, no problem. Apologies for the formatting, but the basic idea should be clear. I also attached my OJB.properties and repository_user.xml.
thanks! Brad
SimpleDagNode class: --------------------------------------- public class SimpleDAGNode{
private List children; private String ID; private List parents;
public SimpleDAGNode() { ID = ""; parents = new Vector(); children = new Vector(); } public String getID() { return ID; } public void addChildNode(DAGNode child) { // Cannot add same child twice if (!children.contains(child)) { children.add(child); if (!child.getParents().contains(this)) { child.getParents().add(this); }
}
} public void removeChild(DAGNode child) { children.remove(child); child.getParents().remove(this); }
public List getParents() { return parents; }
public List getChildren() { return children; }
public boolean equals(Object ref) { if (ref == null) { return false; } if (ref instanceof SimpleDagNode) { return (getID().equals( ( (SimpleDagNode) ref).getID())); } else { return false; } } }
--------------------------------------------------------------------- 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]
--------------------------------------------------------------------- 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]
