Search in sources :

Example 96 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextPrefetchMultistepIT method testToManyToManyFirstStepUnresolved.

@Test
public void testToManyToManyFirstStepUnresolved() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    // since objects for the phantom prefetches are not retained explicitly, they may
    // get garbage collected, and we won't be able to detect them
    // so ensure ObjectStore uses a regular map just for this test
    context.getObjectStore().objectMap = new HashMap<Object, Persistent>();
    // Check the target ArtistExhibit objects do not exist yet
    Map<String, Object> id1 = new HashMap<>();
    id1.put("ARTIST_ID", 11);
    id1.put("EXHIBIT_ID", 2);
    ObjectId oid1 = new ObjectId("ArtistExhibit", id1);
    Map<String, Object> id2 = new HashMap<>();
    id2.put("ARTIST_ID", 101);
    id2.put("EXHIBIT_ID", 2);
    ObjectId oid2 = new ObjectId("ArtistExhibit", id2);
    assertNull(context.getGraphManager().getNode(oid1));
    assertNull(context.getGraphManager().getNode(oid2));
    Expression e = ExpressionFactory.exp("galleryName = $name");
    SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, e.params(Collections.singletonMap("name", "gallery2")));
    q.addPrefetch("exhibitArray.artistExhibitArray");
    List<Gallery> galleries = context.select(q);
    assertEquals(1, galleries.size());
    Gallery g2 = galleries.get(0);
    // this relationship wasn't explicitly prefetched....
    Object list = g2.readPropertyDirectly("exhibitArray");
    assertTrue(list instanceof Fault);
    // however the target objects must be resolved
    ArtistExhibit ae1 = (ArtistExhibit) context.getGraphManager().getNode(oid1);
    ArtistExhibit ae2 = (ArtistExhibit) context.getGraphManager().getNode(oid2);
    assertNotNull(ae1);
    assertNotNull(ae2);
    assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, ae2.getPersistenceState());
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Fault(org.apache.cayenne.Fault) Expression(org.apache.cayenne.exp.Expression) Gallery(org.apache.cayenne.testdo.testmap.Gallery) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) Test(org.junit.Test)

Example 97 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DataContextSharedCacheIT method testSnapshotEvictedForDeleted.

@Test
public void testSnapshotEvictedForDeleted() throws Exception {
    // remember ObjectId
    ObjectId id = artist.getObjectId();
    assertEquals(PersistenceState.COMMITTED, artist.getPersistenceState());
    // delete object PRIOR to killing the snapshot
    context.deleteObjects(artist);
    context.getObjectStore().getDataRowCache().forgetSnapshot(id);
    assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(id));
    context.commitChanges();
    assertEquals(PersistenceState.TRANSIENT, artist.getPersistenceState());
    assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(id));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 98 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DbArcIdTest method testHashCode.

@Test
public void testHashCode() {
    DbArcId id1 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    int h1 = id1.hashCode();
    assertEquals(h1, id1.hashCode());
    assertEquals(h1, id1.hashCode());
    DbArcId id1_eq = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    assertEquals(h1, id1_eq.hashCode());
    DbArcId id2 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r2"));
    assertFalse(h1 == id2.hashCode());
    DbArcId id3 = new DbArcId(new ObjectId("y", "k", "v"), new DbRelationship("r1"));
    assertFalse(h1 == id3.hashCode());
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) Test(org.junit.Test)

Example 99 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class BatchQueryRow method getValue.

/**
 * Used by subclasses to resolve deferred values on demand. This is useful
 * when a certain value comes from a generated key of another master object.
 */
protected Object getValue(Map<String, Object> valueMap, DbAttribute attribute) {
    Object value = valueMap.get(attribute.getName());
    // slight chance that a normal value will implement Factory interface???
    if (value instanceof Supplier) {
        value = ((Supplier) value).get();
        valueMap.put(attribute.getName(), value);
        // update replacement id
        if (attribute.isPrimaryKey()) {
            // sanity check
            if (value == null) {
                String name = attribute.getEntity() != null ? attribute.getEntity().getName() : "<null>";
                throw new CayenneRuntimeException("Failed to generate PK: %s.%s", name, attribute.getName());
            }
            ObjectId id = getObjectId();
            if (id != null) {
                // always override with fresh value as this is what's in the
                // DB
                id.getReplacementIdMap().put(attribute.getName(), value);
            }
        }
    }
    return value;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Supplier(java.util.function.Supplier)

Example 100 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class IdMapKeyAccessor method getValue.

public Object getValue(Object object) throws PropertyException {
    if (object instanceof Persistent) {
        ObjectId id = ((Persistent) object).getObjectId();
        if (id.isTemporary()) {
            return id;
        }
        Map<?, ?> map = id.getIdSnapshot();
        if (map.size() == 1) {
            Map.Entry<?, ?> pkEntry = map.entrySet().iterator().next();
            return pkEntry.getValue();
        }
        return id;
    } else {
        throw new IllegalArgumentException("Object must be Persistent: " + object);
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Map(java.util.Map)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9