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());
}
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));
}
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());
}
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;
}
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);
}
}
Aggregations