Search in sources :

Example 41 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class EntityIdCoderTest method testIdEncoding.

@Test
public void testIdEncoding() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = ObjectId.of("x", "ID", "Ab:C");
    assertEquals("x:Ab%3AC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:Ab%3AC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 42 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class EJBQLIdentifierColumnsTranslator method addPrefetchedColumnsIfAny.

private void addPrefetchedColumnsIfAny(final String visitedIdentifier) {
    PrefetchTreeNode prefetchTree = context.getCompiledExpression().getPrefetchTree();
    if (prefetchTree != null) {
        for (PrefetchTreeNode prefetch : prefetchTree.adjacentJointNodes()) {
            ClassDescriptor descriptor = context.getEntityDescriptor(prefetch.getEjbqlPathEntityId());
            if (visitedIdentifier.equals(prefetch.getEjbqlPathEntityId())) {
                DbEntity table = descriptor.getRootDbEntities().iterator().next();
                ObjEntity objectEntity = descriptor.getEntity();
                prefetch.setEntityName(objectEntity.getName());
                Expression prefetchExp = ExpressionFactory.exp(prefetch.getPath());
                Expression dbPrefetch = objectEntity.translateToDbPath(prefetchExp);
                DbRelationship r = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table.resolvePath(dbPrefetch, context.getMetadata().getPathSplitAliases())) {
                    r = component.getRelationship();
                }
                if (r == null) {
                    throw new CayenneRuntimeException("Invalid joint prefetch '%s' for entity: %s", prefetch, objectEntity.getName());
                }
                for (DbAttribute attribute : r.getTargetEntity().getAttributes()) {
                    appendColumn(prefetch.getEjbqlPathEntityId() + "." + prefetch.getPath(), attribute, "", prefetch.getPath() + "." + attribute.getName(), null);
                }
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 43 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class EJBQLPathTranslator method resolveJoin.

protected void resolveJoin() {
    EJBQLJoinAppender joinAppender = context.getTranslatorFactory().getJoinAppender(context);
    String newPath = idPath + '.' + lastPathComponent;
    String oldPath = joinAppender.registerReusableJoin(idPath, lastPathComponent, newPath);
    this.fullPath = fullPath + '.' + lastPathComponent;
    if (oldPath != null) {
        this.idPath = oldPath;
        Relationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
        if (lastRelationship != null) {
            ObjEntity targetEntity = (ObjEntity) lastRelationship.getTargetEntity();
            this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity.getDbEntity()));
        } else {
            String tableName = context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity.getDbEntity());
            this.lastAlias = context.getTableAlias(oldPath, tableName);
        }
    } else {
        Relationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
        ObjEntity targetEntity = null;
        if (lastRelationship != null) {
            targetEntity = (ObjEntity) lastRelationship.getTargetEntity();
        } else {
            targetEntity = currentEntity;
        }
        // register join
        if (innerJoin) {
            joinAppender.appendInnerJoin(joinMarker, new EJBQLTableId(idPath), new EJBQLTableId(fullPath));
        } else {
            joinAppender.appendOuterJoin(joinMarker, new EJBQLTableId(idPath), new EJBQLTableId(fullPath));
        }
        this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity.getDbEntity()));
        this.idPath = newPath;
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Relationship(org.apache.cayenne.map.Relationship) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 44 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class EJBQLIdColumnsTranslator method visitIdentifier.

@Override
public boolean visitIdentifier(EJBQLExpression expression) {
    Map<String, String> fields = null;
    if (context.isAppendingResultColumns()) {
        fields = context.nextEntityResult().getFields();
    }
    String idVar = expression.getText();
    ClassDescriptor descriptor = context.getEntityDescriptor(idVar);
    ObjEntity oe = descriptor.getEntity();
    for (ObjAttribute oa : oe.getPrimaryKeys()) {
        DbAttribute t = oe.getDbEntity().getAttribute(oa.getDbAttributeName());
        appendColumn(idVar, oa, t, fields, oa.getType());
    }
    return false;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 45 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class CustomColumnSetExtractor method extractFullObject.

private void extractFullObject(String prefix, Property<?> property) {
    prefix = calculatePrefix(prefix, property);
    ensureJoin(prefix);
    ObjEntity entity = context.getResolver().getObjEntity(property.getType());
    ColumnExtractor extractor;
    if (context.getMetadata().getPageSize() > 0) {
        extractor = new IdColumnExtractor(context, entity);
    } else {
        ClassDescriptor descriptor = context.getResolver().getClassDescriptor(entity.getName());
        extractor = new DescriptorColumnExtractor(context, descriptor);
    }
    int index = context.getResultNodeList().size();
    // extract required columns of entity
    extractor.extract(prefix);
    // Maybe we should change resolver, as it seems cleaner to have path from root as prefix in data row key.
    for (int i = index; i < context.getResultNodeList().size(); i++) {
        context.getResultNodeList().get(i).setDataRowKey(null);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11