Search in sources :

Example 1 with ObjectIdQuery

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);
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 2 with ObjectIdQuery

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 3 with ObjectIdQuery

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;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ObjectId(org.apache.cayenne.ObjectId) DataRow(org.apache.cayenne.DataRow) QueryResponse(org.apache.cayenne.QueryResponse) DbRelationship(org.apache.cayenne.map.DbRelationship) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 4 with ObjectIdQuery

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);
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ArrayList(java.util.ArrayList) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 5 with ObjectIdQuery

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"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) Fault(org.apache.cayenne.Fault) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)42 Test (org.junit.Test)34 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)11 ObjectId (org.apache.cayenne.ObjectId)7 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 ToOneFK2 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2)6 DataRow (org.apache.cayenne.DataRow)5 Artist (org.apache.cayenne.testdo.testmap.Artist)5 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)5 ToOneFK1 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1)4 ObjectContext (org.apache.cayenne.ObjectContext)3 Persistent (org.apache.cayenne.Persistent)3 QueryResponse (org.apache.cayenne.QueryResponse)3 SQLTemplate (org.apache.cayenne.query.SQLTemplate)3 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)3 Date (java.util.Date)2 List (java.util.List)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)2 Manager (org.apache.cayenne.testdo.inheritance_people.Manager)2