use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CayenneIT method testObjectForQueryNoObject.
@Test
public void testObjectForQueryNoObject() throws Exception {
ObjectId id = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 44001);
Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
assertNull(object);
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CayenneIT method testObjectForQuery.
@Test
public void testObjectForQuery() throws Exception {
createOneArtist();
ObjectId id = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 33002);
assertNull(context.getGraphManager().getNode(id));
Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
assertNotNull(object);
assertTrue(object instanceof Artist);
assertEquals("artist2", ((Artist) object).getArtistName());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class DeletedDiffProcessor method nodeRemoved.
@Override
public void nodeRemoved(Object nodeId) {
ObjectId id = (ObjectId) nodeId;
final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
// TODO: rewrite with SelectById query after Cayenne upgrade
ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
QueryResponse result = channel.onQuery(null, query);
@SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
if (rows.isEmpty()) {
LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
return;
}
final DataRow row = rows.get(0);
ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
final CommitLogEntity entity = entityFactory.getEntity(id);
descriptor.visitProperties(new PropertyVisitor() {
@Override
public boolean visitAttribute(AttributeProperty property) {
if (!entity.isIncluded(property.getName())) {
return true;
}
Object value;
if (entity.isConfidential(property.getName())) {
value = Confidential.getInstance();
} else {
String key = property.getAttribute().getDbAttributeName();
value = row.get(key);
}
if (value != null) {
objectChangeSet.attributeChanged(property.getName(), value, null);
}
return true;
}
@Override
public boolean visitToOne(ToOneProperty property) {
if (!entity.isIncluded(property.getName())) {
return true;
}
// TODO: is there such a thing as "confidential" relationship that we need to hide?
DbRelationship dbRelationship = property.getRelationship().getDbRelationships().get(0);
ObjectId value = row.createTargetObjectId(property.getTargetDescriptor().getEntity().getName(), dbRelationship);
if (value != null) {
objectChangeSet.toOneRelationshipDisconnected(property.getName(), value);
}
return true;
}
@Override
public boolean visitToMany(ToManyProperty property) {
return true;
}
});
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class BaseContext method prepareForAccess.
@Override
public void prepareForAccess(Persistent object, String property, boolean lazyFaulting) {
if (object.getPersistenceState() == PersistenceState.HOLLOW) {
ObjectId oid = object.getObjectId();
List<?> objects = performQuery(new ObjectIdQuery(oid, false, ObjectIdQuery.CACHE));
if (objects.size() == 0) {
throw new FaultFailureException("Error resolving fault, no matching row exists in the database for ObjectId: " + oid);
} else if (objects.size() > 1) {
throw new FaultFailureException("Error resolving fault, more than one row exists in the database for ObjectId: " + oid);
}
// 5/28/2013 - Commented out this block to allow for modifying
// objects in the postLoad callback
// sanity check...
// if (object.getPersistenceState() != PersistenceState.COMMITTED) {
//
// String state =
// PersistenceState.persistenceStateName(object.getPersistenceState());
//
// // TODO: andrus 4/13/2006, modified and deleted states are
// // possible due to
// // a race condition, should we handle them here?
// throw new
// FaultFailureException("Error resolving fault for ObjectId: " +
// oid + " and state (" + state
// +
// "). Possible cause - matching row is missing from the database.");
// }
}
// resolve relationship fault
if (lazyFaulting && property != null) {
ClassDescriptor classDescriptor = getEntityResolver().getClassDescriptor(object.getObjectId().getEntityName());
PropertyDescriptor propertyDescriptor = classDescriptor.getProperty(property);
// be used.
if (propertyDescriptor == null) {
final StringBuilder errorMessage = new StringBuilder();
errorMessage.append(String.format("Property '%s' is not declared for entity '%s'.", property, object.getObjectId().getEntityName()));
errorMessage.append(" Declared properties are: ");
// Grab each of the declared properties.
final List<String> properties = new ArrayList<>();
classDescriptor.visitProperties(new PropertyVisitor() {
@Override
public boolean visitAttribute(final AttributeProperty property) {
properties.add(property.getName());
return true;
}
@Override
public boolean visitToOne(final ToOneProperty property) {
properties.add(property.getName());
return true;
}
@Override
public boolean visitToMany(final ToManyProperty property) {
properties.add(property.getName());
return true;
}
});
// Now add the declared property names to the error message.
boolean first = true;
for (String declaredProperty : properties) {
if (first) {
errorMessage.append(String.format("'%s'", declaredProperty));
first = false;
} else {
errorMessage.append(String.format(", '%s'", declaredProperty));
}
}
errorMessage.append(".");
throw new CayenneRuntimeException(errorMessage.toString());
}
// this should trigger fault resolving
propertyDescriptor.readProperty(object);
}
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class DataContextIT method testCurrentSnapshotWithToOneFault.
/**
* Testing snapshot with to-one fault. This was a bug CAY-96.
*/
@Test
public void testCurrentSnapshotWithToOneFault() throws Exception {
createGalleriesAndExhibitsDataSet();
// Exhibit with Gallery as Fault must still include Gallery
// Artist and Exhibit (Exhibit has unresolved to-one to gallery as in
// the
// CAY-96 bug report)
ObjectId eId = ObjectId.of("Exhibit", Exhibit.EXHIBIT_ID_PK_COLUMN, 2);
Exhibit e = (Exhibit) context.performQuery(new ObjectIdQuery(eId)).get(0);
assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
DataRow snapshot = context.currentSnapshot(e);
// assert that after taking a snapshot, we have FK in, but the
// relationship
// is still a Fault
assertTrue(e.readPropertyDirectly(Exhibit.TO_GALLERY.getName()) instanceof Fault);
assertEquals(33002, snapshot.get("GALLERY_ID"));
}
Aggregations