use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CDOOneToOneFKIT method testReplaceNull1.
@Test
public void testReplaceNull1() throws Exception {
ToOneFK2 src = context.newObject(ToOneFK2.class);
context.commitChanges();
// test database data
ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
assertEquals(src.getObjectId(), src2.getObjectId());
// *** TESTING THIS ***
src2.setToOneToFK(null);
assertNull(src2.getToOneToFK());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CDOOneToOneFKIT method testReplaceNull2.
@Test
public void testReplaceNull2() throws Exception {
ToOneFK2 src = context.newObject(ToOneFK2.class);
context.commitChanges();
ToOneFK1 target = context.newObject(ToOneFK1.class);
// *** TESTING THIS ***
src.setToOneToFK(target);
// test before save
assertSame(target, src.getToOneToFK());
// do save
context.commitChanges();
// test database data
ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
ToOneFK1 target2 = src2.getToOneToFK();
assertNotNull(target2);
assertEquals(src.getObjectId(), src2.getObjectId());
assertEquals(target.getObjectId(), target2.getObjectId());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CDOOneToOneFKIT method testNewAdd.
@Test
public void testNewAdd() throws Exception {
ToOneFK2 src = context.newObject(ToOneFK2.class);
ToOneFK1 target = context.newObject(ToOneFK1.class);
// *** TESTING THIS ***
src.setToOneToFK(target);
// test before save
assertSame(target, src.getToOneToFK());
// do save
context.commitChanges();
// test database data
ObjectIdQuery refetch = new ObjectIdQuery(src.getObjectId(), false, ObjectIdQuery.CACHE_REFRESH);
ToOneFK2 src2 = (ToOneFK2) Cayenne.objectForQuery(context1, refetch);
ToOneFK1 target2 = src2.getToOneToFK();
assertNotNull(target2);
assertEquals(src.getObjectId(), src2.getObjectId());
assertEquals(target.getObjectId(), target2.getObjectId());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class ChildDiffLoader method findObject.
protected Persistent findObject(Object nodeId) {
// first do a lookup in ObjectStore; if even a hollow object is found,
// return it;
// if not - fetch.
Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
if (object != null) {
return object;
}
ObjectId id = (ObjectId) nodeId;
// modified
if (id.isTemporary()) {
return null;
}
// skip context cache lookup, go directly to its channel
Query query = new ObjectIdQuery((ObjectId) nodeId);
QueryResponse response = context.getChannel().onQuery(context, query);
List<?> objects = response.firstList();
if (objects.size() == 0) {
throw new CayenneRuntimeException("No object for ID exists: %s", nodeId);
} else if (objects.size() > 1) {
throw new CayenneRuntimeException("Expected zero or one object, instead query matched: %d", objects.size());
}
return (Persistent) objects.get(0);
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class DataDomainQueryAction method interceptOIDQuery.
private boolean interceptOIDQuery() {
if (query instanceof ObjectIdQuery) {
ObjectIdQuery oidQuery = (ObjectIdQuery) query;
ObjectId oid = oidQuery.getObjectId();
// translate and run the query. See for instance CAY-1651
if (oid.isTemporary() && !oid.isReplacementIdAttached()) {
response = new ListResponse();
return DONE;
}
DataRow row = null;
if (cache != null && !oidQuery.isFetchMandatory()) {
row = polymorphicRowFromCache(oid);
}
// refresh is forced or not found in cache
if (row == null) {
if (oidQuery.isFetchAllowed()) {
runQueryInTransaction();
} else {
response = new ListResponse();
}
} else {
response = new ListResponse(row);
}
return DONE;
}
return !DONE;
}
Aggregations