Search in sources :

Example 1 with IdCoder

use of org.apache.cayenne.lifecycle.id.IdCoder in project cayenne by apache.

the class ObjectIdRelationshipHandlerTest method testRelate_Change.

@Test
public void testRelate_Change() throws Exception {
    e1Table.insert(1);
    rootTable.insert(1, "E1:1");
    ObjectContext context = runtime.newContext();
    UuidRoot1 r1 = Cayenne.objectForPK(context, UuidRoot1.class, 1);
    assertEquals("E1:1", r1.getUuid());
    E1 e1 = context.newObject(E1.class);
    IdCoder refHandler = new IdCoder(context.getEntityResolver());
    ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler);
    handler.relate(r1, e1);
    assertSame(e1, r1.readPropertyDirectly("cay:related:uuid"));
    context.commitChanges();
    int id = Cayenne.intPKForObject(e1);
    assertFalse(1 == id);
    Object[] r1x = rootTable.select();
    assertEquals("E1:" + id, r1x[1]);
    assertEquals("E1:" + id, r1.getUuid());
    assertSame(e1, r1.readProperty("cay:related:uuid"));
}
Also used : IdCoder(org.apache.cayenne.lifecycle.id.IdCoder) UuidRoot1(org.apache.cayenne.lifecycle.db.UuidRoot1) ObjectContext(org.apache.cayenne.ObjectContext) E1(org.apache.cayenne.lifecycle.db.E1) Test(org.junit.Test)

Example 2 with IdCoder

use of org.apache.cayenne.lifecycle.id.IdCoder in project cayenne by apache.

the class ObjectIdRelationshipHandlerTest method testRelate_Existing.

@Test
public void testRelate_Existing() throws Exception {
    e1Table.insert(1);
    ObjectContext context = runtime.newContext();
    E1 e1 = (E1) Cayenne.objectForQuery(context, new SelectQuery<>(E1.class));
    UuidRoot1 r1 = context.newObject(UuidRoot1.class);
    IdCoder refHandler = new IdCoder(context.getEntityResolver());
    ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler);
    handler.relate(r1, e1);
    assertEquals("E1:1", r1.getUuid());
    assertSame(e1, r1.readPropertyDirectly("cay:related:uuid"));
    context.commitChanges();
    Object[] r1x = rootTable.select();
    assertEquals("E1:1", r1x[1]);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) IdCoder(org.apache.cayenne.lifecycle.id.IdCoder) UuidRoot1(org.apache.cayenne.lifecycle.db.UuidRoot1) ObjectContext(org.apache.cayenne.ObjectContext) E1(org.apache.cayenne.lifecycle.db.E1) Test(org.junit.Test)

Example 3 with IdCoder

use of org.apache.cayenne.lifecycle.id.IdCoder in project cayenne by apache.

the class ObjectIdRelationshipHandlerTest method testRelate_New.

@Test
public void testRelate_New() throws Exception {
    ObjectContext context = runtime.newContext();
    E1 e1 = context.newObject(E1.class);
    UuidRoot1 r1 = context.newObject(UuidRoot1.class);
    IdCoder refHandler = new IdCoder(context.getEntityResolver());
    ObjectIdRelationshipHandler handler = new ObjectIdRelationshipHandler(refHandler);
    handler.relate(r1, e1);
    assertSame(e1, r1.readPropertyDirectly("cay:related:uuid"));
    context.commitChanges();
    int id = Cayenne.intPKForObject(e1);
    Object[] r1x = rootTable.select();
    assertEquals("E1:" + id, r1x[1]);
    assertEquals("E1:" + id, r1.getUuid());
}
Also used : IdCoder(org.apache.cayenne.lifecycle.id.IdCoder) UuidRoot1(org.apache.cayenne.lifecycle.db.UuidRoot1) ObjectContext(org.apache.cayenne.ObjectContext) E1(org.apache.cayenne.lifecycle.db.E1) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)3 E1 (org.apache.cayenne.lifecycle.db.E1)3 UuidRoot1 (org.apache.cayenne.lifecycle.db.UuidRoot1)3 IdCoder (org.apache.cayenne.lifecycle.id.IdCoder)3 Test (org.junit.Test)3 SelectQuery (org.apache.cayenne.query.SelectQuery)1