Search in sources :

Example 1 with SetToMany

use of org.apache.cayenne.testdo.relationships_set_to_many.SetToMany in project cayenne by apache.

the class CDOSetRelationshipIT method testAddToMany.

@Test
public void testAddToMany() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    Set targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    SetToManyTarget newTarget = o1.getObjectContext().newObject(SetToManyTarget.class);
    o1.addToTargets(newTarget);
    assertEquals(4, targets.size());
    assertTrue(o1.getTargets().contains(newTarget));
    assertSame(o1, newTarget.getSetToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : Set(java.util.Set) RefreshQuery(org.apache.cayenne.query.RefreshQuery) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) SetToManyTarget(org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget) Test(org.junit.Test)

Example 2 with SetToMany

use of org.apache.cayenne.testdo.relationships_set_to_many.SetToMany in project cayenne by apache.

the class CayenneDataObjectSetToManySetIT method testRelationCollectionTypeMap.

/**
 * Testing if collection type is set, everything should work fine without an
 * runtimexception
 *
 * @throws Exception
 */
@Test
public void testRelationCollectionTypeMap() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    assertTrue(o1.readProperty(SetToMany.TARGETS.getName()) instanceof Set);
    boolean catchedSomething = false;
    try {
        o1.setToManyTarget(SetToMany.TARGETS.getName(), new ArrayList<MapToMany>(0), true);
    } catch (RuntimeException e) {
        catchedSomething = true;
    }
    assertEquals(catchedSomething, false);
    assertEquals(o1.getTargets().size(), 0);
}
Also used : Set(java.util.Set) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) MapToMany(org.apache.cayenne.testdo.map_to_many.MapToMany) Test(org.junit.Test)

Example 3 with SetToMany

use of org.apache.cayenne.testdo.relationships_set_to_many.SetToMany in project cayenne by apache.

the class CDOSetRelationshipIT method testReadToMany.

@Test
public void testReadToMany() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    Set targets = o1.getTargets();
    assertNotNull(targets);
    assertTrue(((ValueHolder) targets).isFault());
    assertEquals(3, targets.size());
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 1)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 2)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 3)));
}
Also used : Set(java.util.Set) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) Test(org.junit.Test)

Example 4 with SetToMany

use of org.apache.cayenne.testdo.relationships_set_to_many.SetToMany in project cayenne by apache.

the class CDOSetRelationshipIT method testReadToManyPrefetching.

@Test
public void testReadToManyPrefetching() throws Exception {
    createTestDataSet();
    SelectQuery query = new SelectQuery(SetToMany.class, ExpressionFactory.matchDbExp(SetToMany.ID_PK_COLUMN, new Integer(1)));
    query.addPrefetch(SetToMany.TARGETS.disjoint());
    SetToMany o1 = (SetToMany) Cayenne.objectForQuery(context, query);
    Set targets = o1.getTargets();
    assertFalse(((ValueHolder) targets).isFault());
    assertNotNull(targets);
    assertEquals(3, targets.size());
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 1)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 2)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 3)));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Set(java.util.Set) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) Test(org.junit.Test)

Example 5 with SetToMany

use of org.apache.cayenne.testdo.relationships_set_to_many.SetToMany in project cayenne by apache.

the class CDOSetRelationshipIT method testRemoveToMany.

@Test
public void testRemoveToMany() throws Exception {
    createTestDataSet();
    SetToMany o1 = Cayenne.objectForPK(context, SetToMany.class, 1);
    Set targets = o1.getTargets();
    assertEquals(3, targets.size());
    SetToManyTarget target = Cayenne.objectForPK(o1.getObjectContext(), SetToManyTarget.class, 2);
    o1.removeFromTargets(target);
    assertEquals(2, targets.size());
    assertFalse(o1.getTargets().contains(target));
    assertNull(target.getSetToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(2, o1.getTargets().size());
    assertFalse(o1.getTargets().contains(target));
}
Also used : Set(java.util.Set) RefreshQuery(org.apache.cayenne.query.RefreshQuery) SetToMany(org.apache.cayenne.testdo.relationships_set_to_many.SetToMany) SetToManyTarget(org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget) Test(org.junit.Test)

Aggregations

Set (java.util.Set)6 SetToMany (org.apache.cayenne.testdo.relationships_set_to_many.SetToMany)6 Test (org.junit.Test)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)3 SetToManyTarget (org.apache.cayenne.testdo.relationships_set_to_many.SetToManyTarget)3 SelectQuery (org.apache.cayenne.query.SelectQuery)1 MapToMany (org.apache.cayenne.testdo.map_to_many.MapToMany)1