Search in sources :

Example 36 with Persistent

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

the class DataContextEJBQLOrderByIT method testOrderByDesc.

@Test
public void testOrderByDesc() throws Exception {
    createThreePaintings();
    String ejbql1 = "SELECT p FROM Painting p ORDER BY p.paintingTitle DESC";
    EJBQLQuery query1 = new EJBQLQuery(ejbql1);
    List<?> results1 = context.performQuery(query1);
    assertEquals(3, results1.size());
    assertEquals(33003, Cayenne.intPKForObject((Persistent) results1.get(0)));
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results1.get(1)));
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results1.get(2)));
    String ejbql2 = "SELECT p FROM Painting p ORDER BY p.estimatedPrice DESC";
    EJBQLQuery query2 = new EJBQLQuery(ejbql2);
    List<?> results2 = context.performQuery(query2);
    assertEquals(3, results2.size());
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results2.get(0)));
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results2.get(1)));
    assertEquals(33003, Cayenne.intPKForObject((Persistent) results2.get(2)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 37 with Persistent

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

the class DataContextEJBQLOrderByIT method testOrderByQualified.

@Test
public void testOrderByQualified() throws Exception {
    createThreePaintings();
    String ejbql1 = "SELECT p FROM Painting p WHERE p.estimatedPrice > 1000 ORDER BY p.paintingTitle ASC";
    EJBQLQuery query1 = new EJBQLQuery(ejbql1);
    List<?> results1 = context.performQuery(query1);
    assertEquals(2, results1.size());
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results1.get(0)));
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results1.get(1)));
    String ejbql2 = "SELECT p FROM Painting p WHERE p.estimatedPrice > 1000 ORDER BY p.estimatedPrice ASC";
    EJBQLQuery query2 = new EJBQLQuery(ejbql2);
    List<?> results2 = context.performQuery(query2);
    assertEquals(2, results2.size());
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results2.get(0)));
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results2.get(1)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 38 with Persistent

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

the class DataContextEJBQLOrderByIT method testOrderByDefault.

@Test
public void testOrderByDefault() throws Exception {
    createThreePaintings();
    String ejbql1 = "SELECT p FROM Painting p ORDER BY p.paintingTitle";
    EJBQLQuery query1 = new EJBQLQuery(ejbql1);
    List<?> results1 = context.performQuery(query1);
    assertEquals(3, results1.size());
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results1.get(0)));
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results1.get(1)));
    assertEquals(33003, Cayenne.intPKForObject((Persistent) results1.get(2)));
    String ejbql2 = "SELECT p FROM Painting p ORDER BY p.estimatedPrice";
    EJBQLQuery query2 = new EJBQLQuery(ejbql2);
    List<?> results2 = context.performQuery(query2);
    assertEquals(3, results2.size());
    assertEquals(33003, Cayenne.intPKForObject((Persistent) results2.get(0)));
    assertEquals(33002, Cayenne.intPKForObject((Persistent) results2.get(1)));
    assertEquals(33001, Cayenne.intPKForObject((Persistent) results2.get(2)));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 39 with Persistent

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

the class ObjectContextQueryAction method interceptObjectConversion.

/**
 * Transfers fetched objects into the target context if it is different from "acting"
 * context. Note that when this method is invoked, result objects are already
 * registered with acting context by the parent channel.
 */
protected void interceptObjectConversion() {
    if (targetContext != null && !metadata.isFetchingDataRows()) {
        // rewrite response to contain objects from the query context
        GenericResponse childResponse = new GenericResponse();
        ShallowMergeOperation merger = null;
        for (response.reset(); response.next(); ) {
            if (response.isList()) {
                List objects = response.currentList();
                if (objects.isEmpty()) {
                    childResponse.addResultList(objects);
                } else {
                    // minor optimization, skip Object[] if there are no persistent objects
                    boolean haveObjects = metadata.getResultSetMapping() == null;
                    if (!haveObjects) {
                        for (Object next : metadata.getResultSetMapping()) {
                            if (next instanceof EntityResultSegment) {
                                haveObjects = true;
                                break;
                            }
                        }
                    }
                    if (merger == null) {
                        merger = new ShallowMergeOperation(targetContext);
                    }
                    // TODO: Andrus 1/31/2006 - IncrementalFaultList is not properly
                    // transferred between contexts....
                    List<Object> childObjects = new ArrayList<>(objects.size());
                    for (Object object1 : objects) {
                        if (object1 instanceof Persistent) {
                            Persistent object = (Persistent) object1;
                            childObjects.add(merger.merge(object));
                        } else if (haveObjects && object1 instanceof Object[]) {
                            // merge objects inside Object[]
                            Object[] parentData = (Object[]) object1;
                            Object[] childData = new Object[parentData.length];
                            System.arraycopy(parentData, 0, childData, 0, parentData.length);
                            for (int i = 0; i < childData.length; i++) {
                                if (childData[i] instanceof Persistent) {
                                    childData[i] = merger.merge((Persistent) childData[i]);
                                }
                            }
                            childObjects.add(childData);
                        } else {
                            childObjects.add(object1);
                        }
                    }
                    childResponse.addResultList(childObjects);
                }
            } else {
                childResponse.addBatchUpdateCount(response.currentUpdateCount());
            }
        }
        response = childResponse;
    }
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Persistent(org.apache.cayenne.Persistent) EntityResultSegment(org.apache.cayenne.query.EntityResultSegment)

Example 40 with Persistent

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

the class ObjectContextQueryAction method interceptRelationshipQuery.

protected boolean interceptRelationshipQuery() {
    if (query instanceof RelationshipQuery) {
        RelationshipQuery relationshipQuery = (RelationshipQuery) query;
        if (!relationshipQuery.isRefreshing()) {
            if (targetContext == null && relationshipQuery.getRelationship(actingContext.getEntityResolver()).isToMany()) {
                return !DONE;
            }
            ObjectId id = relationshipQuery.getObjectId();
            Object object = actingContext.getGraphManager().getNode(id);
            if (object != null) {
                ClassDescriptor descriptor = actingContext.getEntityResolver().getClassDescriptor(id.getEntityName());
                if (!descriptor.isFault(object)) {
                    ArcProperty property = (ArcProperty) descriptor.getProperty(relationshipQuery.getRelationshipName());
                    if (!property.isFault(object)) {
                        Object related = property.readPropertyDirectly(object);
                        List result;
                        // null to-one
                        if (related == null) {
                            result = new ArrayList(1);
                        } else // to-many List
                        if (related instanceof List) {
                            result = (List) related;
                        } else // to-many Set
                        if (related instanceof Set) {
                            result = new ArrayList((Set) related);
                        } else // to-many Map
                        if (related instanceof Map) {
                            result = new ArrayList(((Map) related).values());
                        } else // non-null to-one
                        {
                            result = new ArrayList(1);
                            result.add(related);
                        }
                        this.response = new ListResponse(result);
                        return DONE;
                    }
                    /**
                     * Workaround for CAY-1183. If a Relationship query is being sent
                     * from child context, we assure that local object is not NEW and
                     * relationship - unresolved (this way exception will occur). This
                     * helps when faulting objects that were committed to parent
                     * context (this), but not to database. Checking type of context's
                     * channel is the only way to ensure that we are on the top level
                     * of context hierarchy (there might be more than one-level-deep
                     * nested contexts).
                     */
                    if (((Persistent) object).getPersistenceState() == PersistenceState.NEW && !(actingContext.getChannel() instanceof BaseContext)) {
                        this.response = new ListResponse();
                        return DONE;
                    }
                }
            }
        }
    }
    return !DONE;
}
Also used : BaseContext(org.apache.cayenne.BaseContext) ArcProperty(org.apache.cayenne.reflect.ArcProperty) Set(java.util.Set) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Persistent(org.apache.cayenne.Persistent) Map(java.util.Map)

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