You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2005/12/04 03:15:58 UTC
cvs commit: db-ojb/src/test/org/apache/ojb repository_junit_odmg.xml repository_junit_reference.xml
arminw 2005/12/03 18:15:58
Modified: src/schema Tag: OJB_1_0_RELEASE ojbtest-schema.xml
src/test/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
CollectionTest.java
src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
BidirectionalAssociationTest.java CircularTest.java
src/test/org/apache/ojb Tag: OJB_1_0_RELEASE
repository_junit_odmg.xml
repository_junit_reference.xml
Log:
update tests, add new tests, update DB table schema
Revision Changes Path
No revision
No revision
1.80.2.30 +3 -1 db-ojb/src/schema/ojbtest-schema.xml
Index: ojbtest-schema.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
retrieving revision 1.80.2.29
retrieving revision 1.80.2.30
diff -u -r1.80.2.29 -r1.80.2.30
--- ojbtest-schema.xml 13 Nov 2005 15:45:54 -0000 1.80.2.29
+++ ojbtest-schema.xml 4 Dec 2005 02:15:47 -0000 1.80.2.30
@@ -1232,11 +1232,13 @@
<table name="COL_BOOKS">
<column name="PK" required="true" primaryKey="true" type="INTEGER"/>
+ <column name="NAME" type="VARCHAR" size="100"/>
<column name="BOOKSHELF_FK" required="true" primaryKey="true" type="INTEGER"/>
</table>
<table name="COL_DVDS">
<column name="PK" required="true" primaryKey="true" type="INTEGER"/>
+ <column name="NAME" type="VARCHAR" size="100"/>
<column name="BOOKSHELF_FK" required="true" primaryKey="true" type="INTEGER"/>
</table>
No revision
No revision
1.11.2.5 +124 -32 db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java
Index: CollectionTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java,v
retrieving revision 1.11.2.4
retrieving revision 1.11.2.5
diff -u -r1.11.2.4 -r1.11.2.5
--- CollectionTest.java 17 Apr 2005 13:29:37 -0000 1.11.2.4
+++ CollectionTest.java 4 Dec 2005 02:15:47 -0000 1.11.2.5
@@ -9,10 +9,12 @@
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
+import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.ojb.broker.query.QueryFactory;
+import org.apache.ojb.broker.util.ObjectModification;
import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
import org.apache.ojb.junit.PBTestCase;
@@ -68,12 +70,83 @@
*/
public class CollectionTest extends PBTestCase
{
+ static final int NONE = ObjectReferenceDescriptor.CASCADE_NONE;
+ static final int LINK = ObjectReferenceDescriptor.CASCADE_LINK;
+ static final int OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
+
public static void main(String[] args)
{
String[] arr = {CollectionTest.class.getName()};
junit.textui.TestRunner.main(arr);
}
+ public void testMoveProxyCollectionFromOneToAnother() throws Exception
+ {
+ String prefix = "testMoveProxyCollectionFromOneToAnother_" + System.currentTimeMillis();
+
+ ojbChangeReferenceSetting(BookShelf.class, "items", true, OBJECT, OBJECT, true);
+ ojbChangeReferenceSetting(DVD.class, "shelf", true, NONE, NONE, true);
+ ojbChangeReferenceSetting(Book.class, "shelf", true, NONE, NONE, true);
+
+ BookShelf bookShelf = new BookShelf(prefix);
+ BookShelf bookShelfSecond = new BookShelf(prefix+"_second");
+ BookShelfItem ev1 = new DVD(prefix+ "_dvd", bookShelf);
+ BookShelfItem ev2 = new Book(prefix + "_book", bookShelf);
+ bookShelf.addItem(ev1);
+ bookShelf.addItem(ev2);
+
+ broker.beginTransaction();
+ broker.store(bookShelfSecond);
+ broker.store(bookShelf);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ BookShelf loadedCopy = (BookShelf) broker.getObjectByIdentity(
+ broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPk()));
+ assertNotNull(loadedCopy);
+ assertNotNull(loadedCopy.getItems());
+ assertEquals(2, loadedCopy.getItems().size());
+
+ broker.beginTransaction();
+ /*
+ now we move the unmaterialzed proxy collection from one to another object,
+ it's important to first store the bookshelf object with the nullified items
+ and then the bookshelf with moved item collection proxy - otherwise the PB-api
+ doesn't recognize the changes
+ */
+ bookShelfSecond.setItems(bookShelf.getItems());
+ bookShelf.setItems(null);
+ broker.store(bookShelf, ObjectModification.UPDATE);
+ broker.store(bookShelfSecond, ObjectModification.UPDATE);
+ broker.commitTransaction();
+ broker.clearCache();
+
+ loadedCopy = (BookShelf) broker.getObjectByIdentity(
+ broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPk()));
+ assertNotNull(loadedCopy);
+ assertNotNull(loadedCopy.getItems());
+ assertEquals(0, loadedCopy.getItems().size());
+
+ BookShelf loadedCopySecond = (BookShelf) broker.getObjectByIdentity(
+ broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelfSecond.getPk()));
+ assertNotNull(loadedCopySecond);
+ assertNotNull(loadedCopySecond.getItems());
+ assertEquals(2, loadedCopySecond.getItems().size());
+
+ broker.clearCache();
+ Criteria criteria = new Criteria();
+ criteria.addLike("name", prefix + "%");
+ Query q = new QueryByCriteria(BookShelfItem.class, criteria);
+ Collection items = broker.getCollectionByQuery(q);
+ assertNotNull(items);
+ assertEquals(2, items.size());
+ // we are using collection proxies, so we have to use the interface
+ BookShelfItem item = (BookShelfItem) items.iterator().next();
+ assertNotNull(item.getShelf());
+ BookShelfIF bs = item.getShelf();
+ assertEquals(bookShelfSecond.getPk(), bs.getPk());
+ }
+
public void testReadProxyCollection() throws Exception
{
String name = "testReadProxyCollection_"+System.currentTimeMillis();
@@ -713,11 +786,11 @@
broker.store(ev1);
broker.store(ev2);
broker.commitTransaction();
- assertTrue(bookShelf.getPK() != null);
+ assertTrue(bookShelf.getPk() != null);
broker.clearCache();
BookShelf loadedCopy = (BookShelf) broker.getObjectByIdentity(
- broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPK()));
+ broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPk()));
assertNotNull(loadedCopy);
assertNotNull(loadedCopy.getItems());
assertEquals(2, loadedCopy.getItems().size());
@@ -751,11 +824,11 @@
broker.store(ev2);
broker.commitTransaction();
- assertTrue(bookShelf.getPK() != null);
+ assertTrue(bookShelf.getPk() != null);
broker.clearCache();
BookShelf loadedCopy = (BookShelf) broker.getObjectByIdentity(
- broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPK()));
+ broker.serviceIdentity().buildIdentity(BookShelf.class, bookShelf.getPk()));
assertNotNull(loadedCopy);
assertNotNull(loadedCopy.getItems());
assertEquals(2, loadedCopy.getItems().size());
@@ -800,7 +873,7 @@
assertNotNull(loadedCopy.getCollectiblesBase());
assertTrue(loadedCopy.getCollectiblesBase() instanceof RemovalAwareCollection);
assertEquals(3, loadedCopy.getCollectiblesBase().size());
-
+
//
// Remove a single element
//
@@ -808,7 +881,7 @@
loadedCopy.getCollectiblesBase().remove(2);
broker.store(loadedCopy);
broker.commitTransaction();
-
+
broker.clearCache();
loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
assertNotNull(loadedCopy);
@@ -823,7 +896,7 @@
loadedCopy.getCollectiblesBase().clear();
broker.store(loadedCopy);
broker.commitTransaction();
-
+
broker.clearCache();
loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
assertNotNull(loadedCopy);
@@ -859,7 +932,7 @@
assertNotNull(loadedCopy.getCollectiblesBase());
assertTrue(loadedCopy.getCollectiblesBase() instanceof RemovalAwareCollection);
assertEquals(2, loadedCopy.getCollectiblesBase().size());
-
+
// add and remove non persistent obj
loadedCopy.getCollectiblesBase().add(new CollectibleBase("Base_3_" + prefix));
assertEquals(3, loadedCopy.getCollectiblesBase().size());
@@ -903,7 +976,7 @@
assertNotNull(loadedCopy.getAllArticlesInGroup());
assertTrue(loadedCopy.getAllArticlesInGroup() instanceof RemovalAwareCollection);
assertEquals(3, loadedCopy.getAllArticlesInGroup().size());
-
+
//
// Remove a single element
//
@@ -911,7 +984,7 @@
loadedCopy.getAllArticlesInGroup().remove(2);
broker.store(loadedCopy);
broker.commitTransaction();
-
+
broker.clearCache();
loadedCopy = (ProductGroup) broker.getObjectByIdentity(pgId);
assertNotNull(loadedCopy);
@@ -926,7 +999,7 @@
loadedCopy.getAllArticlesInGroup().clear();
broker.store(loadedCopy);
broker.commitTransaction();
-
+
broker.clearCache();
loadedCopy = (ProductGroup) broker.getObjectByIdentity(pgId);
assertNotNull(loadedCopy);
@@ -941,78 +1014,71 @@
private CollectibleBase[] prepareCollectibleBase(String namePrefix)
{
- CollectibleBase[] colA = new CollectibleBase[]{
+ return new CollectibleBase[]{
new CollectibleBase(namePrefix + "_colBase_1"),
new CollectibleBase(namePrefix + "_colBase_2"),
new CollectibleBase(namePrefix + "_colBase_3")
};
- return colA;
}
private CollectibleB[] prepareCollectibleB(String namePrefix)
{
- CollectibleB[] colB = new CollectibleB[]{
+ return new CollectibleB[]{
new CollectibleB(namePrefix + "_colB_1"),
new CollectibleB(namePrefix + "_colB_2"),
new CollectibleB(namePrefix + "_colB_3"),
new CollectibleB(namePrefix + "_colB_4")
};
- return colB;
}
private CollectibleC[] prepareCollectibleC(String namePrefix)
{
- CollectibleC[] colC = new CollectibleC[]{
+ return new CollectibleC[]{
new CollectibleC(namePrefix + "_colC_1", "ext1"),
new CollectibleC(namePrefix + "_colC_2", "ext2"),
new CollectibleC(namePrefix + "_colC_3", "ext3"),
new CollectibleC(namePrefix + "_colC_4", "ext4"),
new CollectibleC(namePrefix + "_colC_5", "ext5")
};
- return colC;
}
private CollectibleCC[] prepareCollectibleCC(String namePrefix)
{
- CollectibleCC[] colCC = new CollectibleCC[]{
+ return new CollectibleCC[]{
new CollectibleCC(namePrefix + "_colCC_1", "ext1"),
new CollectibleCC(namePrefix + "_colCC_2", "ext2"),
new CollectibleCC(namePrefix + "_colCC_3", "ext3"),
new CollectibleCC(namePrefix + "_colCC_4", "ext4"),
new CollectibleCC(namePrefix + "_colCC_5", "ext5")
};
- return colCC;
}
private CollectibleC2[] prepareCollectibleC2(String namePrefix)
{
- CollectibleC2[] colCC = new CollectibleC2[]{
+ return new CollectibleC2[]{
new CollectibleC2(namePrefix + "_colC2_1", "ext1"),
new CollectibleC2(namePrefix + "_colC2_2", "ext2"),
new CollectibleC2(namePrefix + "_colC2_3", "ext3"),
new CollectibleC2(namePrefix + "_colC2_4", "ext4"),
new CollectibleC2(namePrefix + "_colC2_5", "ext5")
};
- return colCC;
}
private CollectibleD[] prepareCollectibleD(String namePrefix)
{
- CollectibleD[] colD = new CollectibleD[]{
+ return new CollectibleD[]{
new CollectibleD(namePrefix + "_colD_1"),
new CollectibleD(namePrefix + "_colD_2"),
};
- return colD;
}
private CollectibleDD[] prepareCollectibleDD(String namePrefix)
{
- CollectibleDD[] colDD = new CollectibleDD[]{
+ return new CollectibleDD[]{
new CollectibleDD(namePrefix + "_colDD_1"),
new CollectibleDD(namePrefix + "_colDD_2"),
new CollectibleDD(namePrefix + "_colDD_3")
};
- return colDD;
}
@@ -1438,7 +1504,6 @@
public static interface BookShelfIF
{
- public Integer getPK();
public void addItem(BookShelfItem event);
public List getItems();
public Integer getPk();
@@ -1463,11 +1528,6 @@
this.name = name;
}
- public Integer getPK()
- {
- return pk;
- }
-
public void addItem(BookShelfItem event)
{
if (items == null)
@@ -1476,6 +1536,11 @@
items.add(event);
}
+ public void setItems(List items)
+ {
+ this.items = items;
+ }
+
public List getItems()
{
return items;
@@ -1505,6 +1570,7 @@
public static abstract class BookShelfItem
{
private Integer pk;
+ private String name;
private BookShelfIF shelf;
public BookShelfItem()
@@ -1516,6 +1582,12 @@
this.shelf = shelf;
}
+ protected BookShelfItem(String name, BookShelfIF shelf)
+ {
+ this.name = name;
+ this.shelf = shelf;
+ }
+
public Integer getPk()
{
return pk;
@@ -1526,6 +1598,16 @@
this.pk = pk;
}
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
public BookShelfIF getShelf()
{
return shelf;
@@ -1547,6 +1629,11 @@
{
super(shelf);
}
+
+ public DVD(String name, BookShelfIF shelf)
+ {
+ super(name, shelf);
+ }
}
public static class Book extends BookShelfItem
@@ -1559,5 +1646,10 @@
{
super(shelf);
}
+
+ public Book(String name, BookShelfIF shelf)
+ {
+ super(name, shelf);
+ }
}
}
No revision
No revision
1.7.4.2 +4 -1 db-ojb/src/test/org/apache/ojb/odmg/BidirectionalAssociationTest.java
Index: BidirectionalAssociationTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/BidirectionalAssociationTest.java,v
retrieving revision 1.7.4.1
retrieving revision 1.7.4.2
diff -u -r1.7.4.1 -r1.7.4.2
--- BidirectionalAssociationTest.java 4 Jun 2005 14:48:05 -0000 1.7.4.1
+++ BidirectionalAssociationTest.java 4 Dec 2005 02:15:49 -0000 1.7.4.2
@@ -128,8 +128,11 @@
tx.begin();
b.setRelatedA(a);
a.setRelatedB(b);
- database.makePersistent(a);
+ // we use a FK from ObjectB to ObjectA, thus we
+ // make persistent B
database.makePersistent(b);
+ // not needed
+ //database.makePersistent(a);
tx.commit();
/**
1.1.2.8 +162 -80 db-ojb/src/test/org/apache/ojb/odmg/CircularTest.java
Index: CircularTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/CircularTest.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -r1.1.2.7 -r1.1.2.8
--- CircularTest.java 29 Oct 2005 09:15:06 -0000 1.1.2.7
+++ CircularTest.java 4 Dec 2005 02:15:49 -0000 1.1.2.8
@@ -1,15 +1,5 @@
package org.apache.ojb.odmg;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
-import org.apache.ojb.junit.ODMGTestCase;
-import org.odmg.OQLQuery;
-import org.odmg.Transaction;
-
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +15,17 @@
* limitations under the License.
*/
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
+import org.apache.ojb.broker.metadata.ClassDescriptor;
+import org.apache.ojb.junit.ODMGTestCase;
+import org.odmg.OQLQuery;
+import org.odmg.Transaction;
+
/**
* Testing complex object graphs with circular and bidirectional references when
* using database foreign key settings (without support of deferred foreign keys).
@@ -90,25 +91,25 @@
tx.begin();
p1.addSubProduct(p2);
p2.addSubProduct(p3);
- database.makePersistent(p1);
+ database.makePersistent(p3);
// before establishing the circular references write
// all objects to DB
tx.flush();
// now close the circular references
- p2.addSubProduct(p1);
+ p3.addSubProduct(p1);
tx.commit();
tx.begin();
// on delete break the circular references first, then delete the
// start object
- tx.lock(p2, Transaction.WRITE);
+ tx.lock(p3, Transaction.WRITE);
+ // this call is only needed if auto-delete setting in repository is 'object'
tx.setCascadingDelete(Product.class, "subProducts", false);
- p2.setSubProducts(null);
+ p3.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);
+ database.deletePersistent(p2);
+ database.deletePersistent(p1);
tx.commit();
tx.begin();
@@ -180,6 +181,55 @@
}
/**
+ * Handling circular 1:n references with FK settings and use of
+ * auto-delete setting to delete object graph.
+ */
+ public void testCircularOneToN_3() throws Exception
+ {
+ String name = "testCircularOneToN_3_" + 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(p3);
+ // before establishing the circular references write
+ // all objects to DB
+ tx.flush();
+ // now close the circular references
+ p3.addSubProduct(p1);
+ tx.commit();
+
+ tx.begin();
+ // on delete break the circular references first, then delete the
+ // start object
+ tx.lock(p3, Transaction.WRITE);
+ // this call is only needed if auto-delete setting in repository is 'object'
+ tx.setCascadingDelete(Product.class, "subProducts", false);
+ p3.setSubProducts(null);
+ tx.flush();
+ // this call is only needed if auto-delete setting in repository is 'none'
+ // to enable cascade delete, else we have to delete each object by hand
+ 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();
+
+ assertEquals(0, result.size());
+ }
+
+ /**
* Use auto-delete setting to delete object graph.
*/
public void testCircularWithAutoDeleteEnabled() throws Exception
@@ -355,30 +405,29 @@
}
/**
- * Test show handling with circular references and database FK settings.
+ * Handle circuler 1:1 with default methods.
*/
public void testBidirectionalWithConstraint_1a() throws Exception
{
String name = "testBidirectionalWithConstraint_1a_" + System.currentTimeMillis();
- TransactionExt tx = (TransactionExt) odmg.newTransaction();
- tx.begin();
Shop s1 = new Shop(name + "_1");
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
sd.setShop(s1);
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
database.makePersistent(s1);
tx.commit();
tx.begin();
database.deletePersistent(s1);
- database.deletePersistent(sd);
tx.commit();
}
/**
- * If the user take care of the ordering itself the test pass.
+ * Define order of object operations using flush() method.
*/
public void testBidirectionalWithConstraint_1b() throws Exception
{
@@ -404,8 +453,10 @@
tx.begin();
// madatory to mark object with DB FK constraint first on delete
- // then OJB will use this order to delete the bidirectional objects
+ // (FK from Shop to ShopDetail) then OJB will use this order to
+ // delete the bidirectional objects
database.deletePersistent(s1);
+ tx.flush();
database.deletePersistent(sd);
tx.commit();
}
@@ -417,13 +468,18 @@
{
String name = "testBidirectionalWithConstraint_1c_" + System.currentTimeMillis();
TransactionExt tx = (TransactionExt) odmg.newTransaction();
- tx.begin();
Shop s1 = new Shop(name + "_1");
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
sd.setShop(s1);
+ // set implicit locking false to determine order of objects
+ tx.setImplicitLocking(false);
+ // to prevent reordering of object, disable ordering
+ // in many cases this is not needed, because OJB will leave ordering
+ // tx.setOrdering(false);
+ tx.begin();
// madatory to persist referenced ShopDetail first, the Shop
// object will be detected automatic. In this case first the ShopDetail
// will be created and then the Shop
@@ -431,6 +487,7 @@
database.makePersistent(s1);
tx.commit();
+ // we using the same tx, thus locking and (ordering) is still disabled
tx.begin();
// madatory to mark object with DB FK constraint first on delete
// then OJB will use this order to delete the bidirectional objects
@@ -474,6 +531,55 @@
}
/**
+ * Handle circular 1:1 by using a 'constraint'-flag property in
+ * reference-descriptor to make OJB's ordering algorithm more
+ * sophisticated.
+ */
+ public void testBidirectionalWithConstraint_1e() throws Exception
+ {
+ String name = "testBidirectionalWithConstraint_1e_" + System.currentTimeMillis();
+ ObjectReferenceDescriptor ord = null;
+
+ try
+ {
+ CircularTest.Shop s1 = new CircularTest.Shop(name + "_1");
+ CircularTest.ShopDetail sd = new CircularTest.ShopDetail(name + "_1");
+ s1.setDetail(sd);
+ sd.setShop(s1);
+
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ // now we tell OJB that one 1:1 reference of the bidirectional 1:1 reference
+ // between Shop and ShopDetail has a FK constraint
+ ClassDescriptor cld = tx.getBroker().getClassDescriptor(CircularTest.Shop.class);
+ ord = cld.getObjectReferenceDescriptorByName("detail");
+ // current DB schema create a foreign key constraint and we can
+ // inform OJB
+ ord.setConstraint(true);
+ // now it doesn't matter in which order we persist the new objects, OJB should
+ // always reorder the objects before insert/update call
+ database.makePersistent(sd);
+ // or
+ // database.makePersistent(s1);
+ tx.commit();
+
+ tx.begin();
+ // with cascading delete and the declared FK constraint OJB
+ // always use the correct order on delete.
+ tx.setCascadingDelete(CircularTest.ShopDetail.class, true);
+ database.deletePersistent(sd);
+ // or
+ // database.deletePersistent(s1);
+ tx.commit();
+ }
+ finally
+ {
+ // restore old setting
+ if(ord != null) ord.setConstraint(false);
+ }
+ }
+
+ /**
* Test show handling with circular references and database FK settings.
*/
public void testCircularOneToOne_1a() throws Exception
@@ -630,7 +736,7 @@
*/
tx.setOrdering(false);
tx.setImplicitLocking(false);
-
+
database.makePersistent(aaaa);
database.makePersistent(aaa);
database.makePersistent(aa);
@@ -825,15 +931,12 @@
}
/**
- * If the user take care of the ordering itself the test pass.
+ * User take care of the ordering itself.
*/
public void testCircularOneToOne_2a() throws Exception
{
String name = "testCircularOneToOne_2_" + System.currentTimeMillis();
- TransactionExt tx = (TransactionExt) odmg.newTransaction();
- tx.begin();
-
ObjectA a = new ObjectA(name + "_ObjectA");
ObjectAA aa = new ObjectAA(name + "_ObjectAA");
ObjectAAA aaa = new ObjectAAA(name + "_ObjectAAA");
@@ -841,11 +944,20 @@
a.setRefAA(aa);
aa.setRefAAA(aaa);
aaa.setRefA(a);
+
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ // we want control object insert order itself, thus
+ // disable implicite locking and ordering
+ tx.setImplicitLocking(false);
+ tx.setOrdering(false);
+ tx.begin();
database.makePersistent(aaa);
database.makePersistent(aa);
database.makePersistent(a);
tx.commit();
+ // we use the same tx again, thus implicite locking
+ // and ordering is still disabled
tx.begin();
database.deletePersistent(a);
database.deletePersistent(aa);
@@ -1022,11 +1134,9 @@
}
/**
- * Class Shop has a bidirectional 1:1 reference with ShopDetail and the DB table of Shop
- * has a foreign key constraint on ShopDetail table. Shop has a m:n relation with Distributor.
- * <p/>
- * This test fails!! The ordering isn't able to handle this with causing
- * a key constraint violation.
+ * Class Shop has a bidirectional 1:1 reference with ShopDetail
+ * (FK constraint from SHOP to SHOP_DETAIL table).
+ * Shop has a m:n relation with Distributor.
*/
public void testBidirectionalWithConstraint_2a() throws Exception
{
@@ -1049,6 +1159,8 @@
// touch the Distributor object
tx.begin();
database.deletePersistent(s1);
+ // flush to avoid constraint error
+ tx.flush();
database.deletePersistent(sd);
tx.commit();
}
@@ -1061,62 +1173,30 @@
*/
public void testBidirectionalWithConstraint_2b() throws Exception
{
- String name = "testBidirectionalWithConstraint_2b_" + System.currentTimeMillis();
+ String name = "testBidirectionalWithConstraint_2c_" + System.currentTimeMillis();
Shop s1 = new Shop(name + "_1");
- ShopDetail sd = new ShopDetail(name + "_1");
- s1.setDetail(sd);
- sd.setShop(s1);
Distributor d1 = new Distributor(name + "_1");
s1.addDistributor(d1);
d1.addShop(s1);
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
- // mandatory to first persist the referenced object of the
- // bidirectional 1:1 reference, because of the FK in Shop
- // the m:n relation will be handled without problems
- database.makePersistent(sd);
- // it's not needed to declare all objects in this case,
- // but it wouldn't affect
- // database.makePersistent(s1);
- // database.makePersistent(d1);
- tx.commit();
-
- // Now we delete the Shop with ShopDetail, but don't
- // touch the Distributor object
- tx.begin();
- database.deletePersistent(s1);
- database.deletePersistent(sd);
- tx.commit();
- }
-
- /**
- * Class Shop has a bidirectional 1:1 reference with ShopDetail and the DB table of Shop
- * has a foreign key constraint on ShopDetail table. Shop has a m:n relation with Distributor.
- * <p/>
- * If the user take care of the ordering itself the test pass.
- */
- public void testBidirectionalWithConstraint_2c() throws Exception
- {
- String name = "testBidirectionalWithConstraint_2c_" + System.currentTimeMillis();
- TransactionExt tx = (TransactionExt) odmg.newTransaction();
- tx.begin();
- // When using flush() we can use a more "natural" object persisting order
- Shop s1 = new Shop(name + "_1");
- Distributor d1 = new Distributor(name + "_1");
- s1.addDistributor(d1);
- d1.addShop(s1);
+ // When using flush() we can add objects step by step
database.makePersistent(s1);
tx.flush();
+ // add the shop detail object to Shop
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
sd.setShop(s1);
tx.commit();
- // Delete all created objects
+ // Delete all created objects, we disable implicit
+ // locking and ordering to avoid constraint error
+ tx.setImplicitLocking(false);
+ tx.setOrdering(false);
tx.begin();
database.deletePersistent(d1);
database.deletePersistent(s1);
@@ -1623,12 +1703,11 @@
sd.setName(name);
s.setDetail(sd);
sd.setShop(s);
+
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
- database.makePersistent(sd);
database.makePersistent(s);
database.deletePersistent(s);
- database.makePersistent(sd);
database.makePersistent(s);
tx.commit();
@@ -1644,23 +1723,28 @@
assertNotNull(newShop.getDetail());
tx.begin();
- // We enable cascading delete for all references of Shop class
- tx.setCascadingDelete(Shop.class, true);
database.deletePersistent(newShop);
+ // add object again, we expect that nothing was deleted
database.makePersistent(newShop);
+ // flush changes to DB, we don't change anything
tx.flush();
query = odmg.newOQLQuery();
query.create("select shops from " + Shop.class.getName() + " where name like $1");
query.bind(name);
result = (Collection) query.execute();
tx.commit();
+
assertEquals(1, result.size());
+ Shop tmp = (Shop) result.iterator().next();
+ assertNotNull(tmp.getDetail());
tx.begin();
database.deletePersistent(newShop);
database.makePersistent(newShop);
database.deletePersistent(newShop);
tx.flush();
+ database.deletePersistent(newShop.getDetail());
+ tx.flush();
query = odmg.newOQLQuery();
query.create("select detail from " + ShopDetail.class.getName() + " where name like $1");
@@ -1702,7 +1786,6 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
- database.makePersistent(sd);
database.makePersistent(s);
tx.commit();
@@ -1720,9 +1803,8 @@
assertNotNull(sdNew.getShop());
tx.begin();
- // cascading delete should not affect Shop deletion
+ // cascading delete should delete Shop too
tx.setCascadingDelete(ShopDetail.class, true);
- database.deletePersistent(sdNew.getShop());
database.deletePersistent(sdNew);
tx.flush();
No revision
No revision
1.13.2.19 +8 -8 db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml
Index: repository_junit_odmg.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml,v
retrieving revision 1.13.2.18
retrieving revision 1.13.2.19
diff -u -r1.13.2.18 -r1.13.2.19
--- repository_junit_odmg.xml 26 Nov 2005 02:19:54 -0000 1.13.2.18
+++ repository_junit_odmg.xml 4 Dec 2005 02:15:52 -0000 1.13.2.19
@@ -1958,7 +1958,7 @@
auto-delete="none"
>
<foreignkey field-ref="detailFk"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
<collection-descriptor
@@ -2060,7 +2060,7 @@
auto-delete="none"
>
<foreignkey field-ref="shopFk"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
<collection-descriptor
@@ -2072,7 +2072,7 @@
auto-delete="none"
>
<inverse-foreignkey field-ref="subProductFK"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</collection-descriptor>
</class-descriptor>
@@ -2147,7 +2147,7 @@
auto-delete="none"
>
<foreignkey field-ref="fkId"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
</class-descriptor>
@@ -2185,7 +2185,7 @@
auto-delete="none"
>
<foreignkey field-ref="fkId"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
</class-descriptor>
@@ -2240,7 +2240,7 @@
auto-delete="none"
>
<foreignkey field-ref="fkId"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
</class-descriptor>
@@ -2371,7 +2371,7 @@
auto-update="none"
auto-delete="none">
<foreignkey field-ref="fkToA"/>
- <attribute attribute-name="fk" attribute-value="true"/>
+ <!--<attribute attribute-name="constraint" attribute-value="true"/>-->
</reference-descriptor>
</class-descriptor>
1.17.2.8 +13 -1 db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml
Index: repository_junit_reference.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml,v
retrieving revision 1.17.2.7
retrieving revision 1.17.2.8
diff -u -r1.17.2.7 -r1.17.2.8
--- repository_junit_reference.xml 13 Nov 2005 15:45:55 -0000 1.17.2.7
+++ repository_junit_reference.xml 4 Dec 2005 02:15:52 -0000 1.17.2.8
@@ -1142,6 +1142,12 @@
autoincrement="true"
/>
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
<field-descriptor
name="shelfFk"
column="BOOKSHELF_FK"
@@ -1173,6 +1179,12 @@
autoincrement="true"
/>
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
<field-descriptor
name="shelfFk"
column="BOOKSHELF_FK"
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org