Search in sources :

Example 51 with Persistent

use of org.apache.cayenne.Persistent 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 52 with Persistent

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

the class BaseQueryMetadata method resolve.

boolean resolve(Object root, EntityResolver resolver) {
    if (lastRoot != root || lastEntityResolver != resolver) {
        this.classDescriptor = null;
        this.dbEntity = null;
        this.dataMap = null;
        ObjEntity entity = null;
        if (root != null) {
            if (root instanceof Class<?>) {
                entity = resolver.getObjEntity((Class<?>) root, true);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            } else if (root instanceof ObjEntity) {
                entity = (ObjEntity) root;
                this.dbEntity = entity.getDbEntity();
                this.dataMap = entity.getDataMap();
            } else if (root instanceof String) {
                entity = resolver.getObjEntity((String) root);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            } else if (root instanceof DbEntity) {
                this.dbEntity = (DbEntity) root;
                this.dataMap = dbEntity.getDataMap();
            } else if (root instanceof DataMap) {
                this.dataMap = (DataMap) root;
            } else if (root instanceof Persistent) {
                entity = resolver.getObjEntity((Persistent) root);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            }
        }
        if (entity != null) {
            this.classDescriptor = resolver.getClassDescriptor(entity.getName());
        }
        this.lastRoot = root;
        this.lastEntityResolver = resolver;
        return true;
    }
    return false;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Persistent(org.apache.cayenne.Persistent) DataMap(org.apache.cayenne.map.DataMap)

Example 53 with Persistent

use of org.apache.cayenne.Persistent 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)

Example 54 with Persistent

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

the class SimpleAttributeProperty method resolveFault.

protected void resolveFault(Object object) throws PropertyException {
    try {
        Persistent persistent = (Persistent) object;
        ObjectContext context = persistent.getObjectContext();
        if (context != null) {
            context.prepareForAccess(persistent, getName(), false);
        }
    } catch (ClassCastException e) {
        throw new PropertyException("Object is not a Persistent: '" + object.getClass().getName() + "'", this, object, e);
    }
}
Also used : Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext)

Example 55 with Persistent

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

the class DataContextEJBQLIsNullIT method testIsNotNull.

@Test
public void testIsNotNull() throws Exception {
    createTwoPaintings();
    String ejbql1 = "SELECT p FROM Painting p WHERE p.estimatedPrice IS NOT NULL";
    EJBQLQuery query1 = new EJBQLQuery(ejbql1);
    List<?> results = context.performQuery(query1);
    assertEquals(1, results.size());
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results.get(0)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Aggregations

Persistent (org.apache.cayenne.Persistent)88 ObjectId (org.apache.cayenne.ObjectId)38 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)17 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 DataObject (org.apache.cayenne.DataObject)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)10 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)10 DataRow (org.apache.cayenne.DataRow)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6 List (java.util.List)5