Search in sources :

Example 1 with ClientIdMapToMany

use of org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany in project cayenne by apache.

the class CayenneContextMapRelationshipIT method testReadToMany.

@Test
public void testReadToMany() throws Exception {
    createTwoMapToManysWithTargetsDataSet();
    ObjectId id = ObjectId.of("IdMapToMany", IdMapToMany.ID_PK_COLUMN, 1);
    ClientIdMapToMany o1 = (ClientIdMapToMany) Cayenne.objectForQuery(context, new ObjectIdQuery(id));
    Map<Object, ClientIdMapToManyTarget> targets = o1.getTargets();
    assertTrue(((ValueHolder) targets).isFault());
    assertNotNull(targets);
    assertEquals(3, targets.size());
    assertNotNull(targets.get(1));
    assertNotNull(targets.get(2));
    assertNotNull(targets.get(3));
}
Also used : ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 2 with ClientIdMapToMany

use of org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany in project cayenne by apache.

the class CayenneContextMapRelationshipIT method testAddToMany.

@Test
public void testAddToMany() throws Exception {
    createTwoMapToManysWithTargetsDataSet();
    ObjectId id = ObjectId.of("IdMapToMany", IdMapToMany.ID_PK_COLUMN, 1);
    ClientIdMapToMany o1 = (ClientIdMapToMany) Cayenne.objectForQuery(context, new ObjectIdQuery(id));
    Map<Object, ClientIdMapToManyTarget> targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    ClientIdMapToManyTarget newTarget = o1.getObjectContext().newObject(ClientIdMapToManyTarget.class);
    o1.addToTargets(newTarget);
    assertEquals(4, targets.size());
    assertSame(o1, newTarget.getMapToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
    int newId = Cayenne.intPKForObject(newTarget);
    assertSame(newTarget, o1.getTargets().get(newId));
    assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, newTarget.getPersistenceState());
}
Also used : ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 3 with ClientIdMapToMany

use of org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany in project cayenne by apache.

the class ROPPrefetchToManyMapIT method test.

@Test
public void test() {
    ObjectContext context = createROPContext();
    ClientIdMapToMany map = context.newObject(ClientIdMapToMany.class);
    ClientIdMapToManyTarget target = context.newObject(ClientIdMapToManyTarget.class);
    target.setMapToMany(map);
    context.commitChanges();
    context.performQuery(new RefreshQuery());
    ObjectSelect<ClientIdMapToMany> query = ObjectSelect.query(ClientIdMapToMany.class).prefetch("targets", PrefetchTreeNode.UNDEFINED_SEMANTICS);
    final ClientIdMapToMany mapToMany = (ClientIdMapToMany) Cayenne.objectForQuery(context, query);
    queryInterceptor.runWithQueriesBlocked(() -> assertEquals(mapToMany.getTargets().size(), 1));
}
Also used : ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ClientIdMapToMany (org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany)3 ClientIdMapToManyTarget (org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget)3 Test (org.junit.Test)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)2 RefreshQuery (org.apache.cayenne.query.RefreshQuery)2 ObjectContext (org.apache.cayenne.ObjectContext)1