arminw 2005/08/22 08:34:50
Modified: src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
CircularTest.java
Log:
add tests to demonstrate handling with circular 1:n references
Revision Changes Path
No revision
No revision
1.1.2.6 +131 -2
db-ojb/src/test/org/apache/ojb/odmg/Attic/CircularTest.java
Index: CircularTest.java
===================================================================
RCS file:
/home/cvs/db-ojb/src/test/org/apache/ojb/odmg/Attic/CircularTest.java,v
retrieving revision 1.1.2.5
retrieving revision 1.1.2.6
diff -u -r1.1.2.5 -r1.1.2.6
--- CircularTest.java 4 Jun 2005 14:48:05 -0000 1.1.2.5
+++ CircularTest.java 22 Aug 2005 15:34:50 -0000 1.1.2.6
@@ -26,7 +26,8 @@
*/
/**
- * Testing complex object graphs with circular and bidirectional references.
+ * Testing complex object graphs with circular and bidirectional references
when
+ * using database foreign key settings (without support of deferred foreign
keys).
* <p/>
* The classes hierarchy looks like:
* <br/>
@@ -73,6 +74,112 @@
}
/**
+ * Handling circular 1:n references with FK settings and use of
+ * auto-delete setting to delete object graph.
+ */
+ public void testCircularOneToN_1() throws Exception
+ {
+ String name = "testCircularOneToN_1_" + System.currentTimeMillis();
+ ojbChangeReferenceSetting(Product.class, "subProducts", true,
ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_OBJECT, false);
+
+ Product p1 = new Product(name + "_p1");
+ Product p2 = new Product(name + "_p2");
+ Product p3 = new Product(name + "_p3");
+
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ p1.addSubProduct(p2);
+ p2.addSubProduct(p3);
+ database.makePersistent(p1);
+ // before establishing the circular references write
+ // all objects to DB
+ tx.flush();
+ // now close the circular references
+ p2.addSubProduct(p1);
+ tx.commit();
+
+ tx.begin();
+ // on delete break the circular references first, then delete the
+ // start object
+ tx.lock(p2, Transaction.WRITE);
+ tx.setCascadingDelete(Product.class, "subProducts", false);
+ p2.setSubProducts(null);
+ tx.flush();
+ tx.setCascadingDelete(Product.class, "subProducts", true);
+ database.deletePersistent(p1);
+ // this object was unlinked on fluhs(), so we have to remove it by
hand
+ database.deletePersistent(p3);
+ tx.commit();
+
+ tx.begin();
+ OQLQuery query = odmg.newOQLQuery();
+ query.create("select objects from " + Product.class.getName() + "
where name like $1");
+ query.bind(name + "%");
+ Collection result = (Collection) query.execute();
+ tx.commit();
+
+ assertEquals(0, result.size());
+ }
+
+ /**
+ * Handling circular 1:n references with FK settings and use of
+ * auto-delete setting to delete object graph.
+ */
+ public void testCircularOneToN_2() throws Exception
+ {
+ String name = "testCircularOneToN_2_" + System.currentTimeMillis();
+ ojbChangeReferenceSetting(Product.class, "subProducts", true,
ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_OBJECT, false);
+
+ Product p1 = new Product(name + "_p1");
+ Product p2 = new Product(name + "_p2");
+ Product p3 = new Product(name + "_p3");
+ Product p4 = new Product(name + "_p4");
+ Product p5 = new Product(name + "_p5");
+ Product p6 = new Product(name + "_p6");
+ Product p7 = new Product(name + "_p7");
+
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ p1.addSubProduct(p2);
+ p1.addSubProduct(p3);
+ p3.addSubProduct(p4);
+ p3.addSubProduct(p5);
+ p5.addSubProduct(p6);
+ p6.addSubProduct(p7);
+ database.makePersistent(p1);
+ // before establishing the circular references write
+ // all objects to DB
+ tx.flush();
+ // now close the circular references
+ p6.addSubProduct(p1);
+ tx.commit();
+
+ tx.begin();
+ // on delete break the circular references first, then delete the
+ // start object
+ tx.lock(p6, Transaction.WRITE);
+ tx.setCascadingDelete(Product.class, "subProducts", false);
+ p6.setSubProducts(null);
+ tx.flush();
+ tx.setCascadingDelete(Product.class, "subProducts", true);
+ database.deletePersistent(p1);
+ tx.commit();
+
+ tx.begin();
+ OQLQuery query = odmg.newOQLQuery();
+ query.create("select objects from " + Product.class.getName() + "
where name like $1");
+ query.bind(name + "%");
+ Collection result = (Collection) query.execute();
+ tx.commit();
+
+ // we expect one Product object, because we set cascading delete
'false'
+ // when do 'p6.setSubProducts(null);', so the '..._p7' Product will
only be unlinked
+ assertEquals(1, result.size());
+ Product p7_new = (Product) result.iterator().next();
+ assertEquals(name + "_p7", p7_new.getName());
+ }
+
+ /**
* Use auto-delete setting to delete object graph.
*/
public void testCircularWithAutoDeleteEnabled() throws Exception
@@ -126,6 +233,14 @@
tx.commit();
tx.begin();
+ /*
+ arminw: When deleting a object which is part of circular 1:1
references
+ with cascade delete enabled it's mandatory to break the circular
reference
+ before deleting it.
+ */
+ tx.lock(aaaa, Transaction.WRITE);
+ // break the circular references
+ aaaa.setRefA(null);
database.deletePersistent(a);
tx.commit();
@@ -1862,6 +1977,11 @@
{
}
+ public Product(String name)
+ {
+ this.name = name;
+ }
+
public Integer getId()
{
return id;
@@ -1912,6 +2032,15 @@
this.subProductFK = subProductFK;
}
+ public void addSubProduct(Product p)
+ {
+ if(subProducts == null)
+ {
+ subProducts = new ArrayList();
+ }
+ subProducts.add(p);
+ }
+
public List getSubProducts()
{
return subProducts;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]