Search in sources :

Example 1 with CollectionToManyTarget

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget in project cayenne by apache.

the class CDOCollectionRelationshipIT method testAddToMany.

@Test
public void testAddToMany() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    CollectionToManyTarget newTarget = o1.getObjectContext().newObject(CollectionToManyTarget.class);
    o1.addToTargets(newTarget);
    assertEquals(4, targets.size());
    assertTrue(o1.getTargets().contains(newTarget));
    assertSame(o1, newTarget.getCollectionToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Example 2 with CollectionToManyTarget

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget in project cayenne by apache.

the class CDOCollectionRelationshipIT method testAddToManyViaReverse.

@Test
public void testAddToManyViaReverse() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    CollectionToManyTarget newTarget = o1.getObjectContext().newObject(CollectionToManyTarget.class);
    newTarget.setCollectionToMany(o1);
    assertEquals(4, targets.size());
    assertTrue(o1.getTargets().contains(newTarget));
    assertSame(o1, newTarget.getCollectionToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Example 3 with CollectionToManyTarget

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget in project cayenne by apache.

the class CDOCollectionRelationshipIT method testRemoveToMany.

@Test
public void testRemoveToMany() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertEquals(3, targets.size());
    CollectionToManyTarget target = Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2);
    o1.removeFromTargets(target);
    assertEquals(2, targets.size());
    assertFalse(o1.getTargets().contains(target));
    assertNull(target.getCollectionToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(2, o1.getTargets().size());
    assertFalse(o1.getTargets().contains(target));
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Aggregations

RefreshQuery (org.apache.cayenne.query.RefreshQuery)3 CollectionToMany (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany)3 CollectionToManyTarget (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget)3 Test (org.junit.Test)3