Search in sources :

Example 1 with PersistentObjectHolder

use of org.apache.cayenne.util.PersistentObjectHolder in project cayenne by apache.

the class PersistentObjectInContextIT method testResolveToOneReverseResolved.

@Test
public void testResolveToOneReverseResolved() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectId gid = new ObjectId("MtTable2", MtTable2.TABLE2_ID_PK_COLUMN, new Integer(1));
    ClientMtTable2 mtTable21 = (ClientMtTable2) Cayenne.objectForQuery(context, new ObjectIdQuery(gid));
    assertNotNull(mtTable21);
    ClientMtTable1 mtTable1 = mtTable21.getTable1();
    assertNotNull("To one relationship incorrectly resolved to null", mtTable1);
    List<ClientMtTable2> list = mtTable1.getTable2Array();
    assertNotNull(list);
    assertTrue(list instanceof ValueHolder);
    assertTrue(((ValueHolder) list).isFault());
    // resolve it here...
    assertEquals(2, list.size());
    for (ClientMtTable2 t2 : list) {
        PersistentObjectHolder holder = (PersistentObjectHolder) t2.getTable1Direct();
        assertFalse(holder.isFault());
        assertSame(mtTable1, holder.getValue());
    }
    assertEquals("g1", mtTable1.getGlobalAttribute1());
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 2 with PersistentObjectHolder

use of org.apache.cayenne.util.PersistentObjectHolder in project cayenne by apache.

the class ClientServerChannelQueryIT method testSelectQueryPrefetchToOne.

@Test
public void testSelectQueryPrefetchToOne() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    SelectQuery q = new SelectQuery(ClientMtTable2.class, ExpressionFactory.exp("globalAttribute = 'g1'"));
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    List<?> results = context.performQuery(q);
    assertEquals(1, results.size());
    ClientMtTable2 result = (ClientMtTable2) results.get(0);
    ValueHolder holder = result.getTable1Direct();
    assertNotNull(holder);
    assertTrue(holder instanceof PersistentObjectHolder);
    PersistentObjectHolder objectHolder = (PersistentObjectHolder) holder;
    assertFalse(objectHolder.isFault());
    ClientMtTable1 target = (ClientMtTable1) objectHolder.getValue();
    assertNotNull(target);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 3 with PersistentObjectHolder

use of org.apache.cayenne.util.PersistentObjectHolder in project cayenne by apache.

the class _Painting method setArtist.

public void setArtist(Artist artist) {
    if (objectContext != null) {
        objectContext.prepareForAccess(this, "artist", true);
    } else if (this.artist == null) {
        this.artist = new PersistentObjectHolder(this, "artist");
    }
    // note how we notify ObjectContext of change BEFORE the object is actually
    // changed... this is needed to take a valid current snapshot
    Object oldValue = this.artist.getValueDirectly();
    if (objectContext != null) {
        objectContext.propertyChanged(this, "artist", oldValue, artist);
    }
    this.artist.setValue(artist);
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) PersistentObject(org.apache.cayenne.PersistentObject)

Example 4 with PersistentObjectHolder

use of org.apache.cayenne.util.PersistentObjectHolder in project cayenne by apache.

the class _Painting method setGallery.

public void setGallery(Gallery gallery) {
    if (objectContext != null) {
        objectContext.prepareForAccess(this, "gallery", true);
    } else if (this.gallery == null) {
        this.gallery = new PersistentObjectHolder(this, "gallery");
    }
    // note how we notify ObjectContext of change BEFORE the object is actually
    // changed... this is needed to take a valid current snapshot
    Object oldValue = this.gallery.getValueDirectly();
    if (objectContext != null) {
        objectContext.propertyChanged(this, "gallery", oldValue, gallery);
    }
    this.gallery.setValue(gallery);
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) PersistentObject(org.apache.cayenne.PersistentObject)

Example 5 with PersistentObjectHolder

use of org.apache.cayenne.util.PersistentObjectHolder in project cayenne by apache.

the class PersistentObjectInContextIT method testResolveToManyReverseResolved.

@Test
public void testResolveToManyReverseResolved() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectId gid = new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, new Integer(1));
    ClientMtTable1 t1 = (ClientMtTable1) Cayenne.objectForQuery(context, new ObjectIdQuery(gid));
    assertNotNull(t1);
    List<ClientMtTable2> t2s = t1.getTable2Array();
    assertEquals(2, t2s.size());
    for (ClientMtTable2 t2 : t2s) {
        PersistentObjectHolder holder = (PersistentObjectHolder) t2.getTable1Direct();
        assertFalse(holder.isFault());
        assertSame(t1, holder.getValue());
    }
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Aggregations

PersistentObjectHolder (org.apache.cayenne.util.PersistentObjectHolder)5 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)3 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)3 Test (org.junit.Test)3 PersistentObject (org.apache.cayenne.PersistentObject)2 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)2 ValueHolder (org.apache.cayenne.ValueHolder)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1