Search in sources :

Example 51 with ObjEntity

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

the class PrefetchNodeStage method processJoint.

private void processJoint(TranslatorContext context) {
    QueryMetadata queryMetadata = context.getMetadata();
    PrefetchTreeNode prefetch = queryMetadata.getPrefetchTree();
    if (prefetch == null) {
        return;
    }
    ObjEntity objEntity = queryMetadata.getObjEntity();
    boolean warnPrefetchWithLimit = false;
    for (PrefetchTreeNode node : prefetch.adjacentJointNodes()) {
        Expression prefetchExp = ExpressionFactory.exp(node.getPath());
        ASTDbPath dbPrefetch = (ASTDbPath) objEntity.translateToDbPath(prefetchExp);
        final String dbPath = dbPrefetch.getPath();
        DbEntity dbEntity = objEntity.getDbEntity();
        PathComponents components = new PathComponents(dbPath);
        StringBuilder fullPath = new StringBuilder();
        for (String c : components.getAll()) {
            DbRelationship rel = dbEntity.getRelationship(c);
            if (rel == null) {
                throw new CayenneRuntimeException("Unable to resolve path %s for entity %s", dbPath, objEntity.getName());
            }
            if (fullPath.length() > 0) {
                fullPath.append('.');
            }
            context.getTableTree().addJoinTable("p:" + fullPath.append(c).toString(), rel, JoinType.LEFT_OUTER);
            dbEntity = rel.getTargetEntity();
        }
        ObjRelationship targetRel = (ObjRelationship) prefetchExp.evaluate(objEntity);
        ClassDescriptor prefetchClassDescriptor = context.getResolver().getClassDescriptor(targetRel.getTargetEntityName());
        DescriptorColumnExtractor columnExtractor = new DescriptorColumnExtractor(context, prefetchClassDescriptor);
        columnExtractor.extract("p:" + dbPath);
        if (!warnPrefetchWithLimit && targetRel.isToMany() && (queryMetadata.getFetchLimit() > 0 || queryMetadata.getFetchOffset() > 0)) {
            warnPrefetchWithLimit = true;
        }
    }
    // warn about a potentially faulty joint prefetch + limit combination
    if (warnPrefetchWithLimit) {
        LOGGER.warn("The query uses both limit/offset and a joint prefetch, this most probably will lead to an incorrect result. " + "Either use disjointById prefetch or get a full result set.");
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) QueryMetadata(org.apache.cayenne.query.QueryMetadata) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 52 with ObjEntity

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

the class QualifierTranslationStage method perform.

@Override
public void perform(TranslatorContext context) {
    QualifierTranslator translator = context.getQualifierTranslator();
    Expression expression = context.getQuery().getQualifier();
    // Attaching Obj entity's qualifier
    ObjEntity entity = context.getMetadata().getObjEntity();
    if (entity != null) {
        ClassDescriptor descriptor = context.getMetadata().getClassDescriptor();
        Expression entityQualifier = descriptor.getEntityInheritanceTree().qualifierForEntityAndSubclasses();
        if (entityQualifier != null) {
            expression = expression == null ? entityQualifier : expression.andExp(entityQualifier);
        }
    }
    Node qualifierNode = translator.translate(expression);
    context.setQualifierNode(qualifierNode);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Expression(org.apache.cayenne.exp.Expression) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) SimpleNode(org.apache.cayenne.exp.parser.SimpleNode)

Example 53 with ObjEntity

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

the class ObjPathProcessorIT3 method prepareTranslationContext.

@Before
public void prepareTranslationContext() {
    TranslatorContext translatorContext = new TranslatorContext(new FluentSelectWrapper(ObjectSelect.query(Object.class)), Mockito.mock(DbAdapter.class), context.getEntityResolver(), null);
    ObjEntity entity = context.getEntityResolver().getObjEntity("FlattenedTest5");
    pathProcessor = new ObjPathProcessor(translatorContext, entity, null);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbAdapter(org.apache.cayenne.dba.DbAdapter) Before(org.junit.Before)

Example 54 with ObjEntity

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

the class OrderingStageTest method prepareContext.

@Before
public void prepareContext() {
    DbEntity dbEntity = new DbEntity();
    dbEntity.setName("mock");
    DbAttribute dbAttribute = new DbAttribute();
    dbAttribute.setName("path");
    dbEntity.addAttribute(dbAttribute);
    ObjEntity objEntity = new ObjEntity();
    objEntity.setName("mock");
    objEntity.setDbEntity(dbEntity);
    ObjAttribute objAttribute = new ObjAttribute();
    objAttribute.setName("path");
    objAttribute.setDbAttributePath("path");
    objEntity.addAttribute(objAttribute);
    DataMap dataMap = new DataMap();
    dataMap.addObjEntity(objEntity);
    dataMap.addDbEntity(dbEntity);
    Ordering ordering = new Ordering("path");
    ordering.setDescending();
    TranslatableQueryWrapper wrapper = new MockQueryWrapperBuilder().withOrderings(Collections.singleton(ordering)).withMetaData(new MockQueryMetadataBuilder().withDbEntity(dbEntity).withObjEntity(objEntity).build()).build();
    context = new MockTranslatorContext(wrapper);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) Ordering(org.apache.cayenne.query.Ordering) DataMap(org.apache.cayenne.map.DataMap) Before(org.junit.Before)

Example 55 with ObjEntity

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

the class CustomColumnSetExtractorTest method testExtractWithoutPrefix.

@Test
public void testExtractWithoutPrefix() {
    DbEntity mockDbEntity = createMockDbEntity("mock");
    TranslatableQueryWrapper wrapper = new MockQueryWrapperBuilder().withNeedsResultSetMapping(true).withMetaData(new MockQueryMetadataBuilder().withDbEntity(mockDbEntity).build()).build();
    TranslatorContext context = new MockTranslatorContext(wrapper);
    DataMap dataMap = new DataMap();
    dataMap.addDbEntity(mockDbEntity);
    ObjEntity entity = new ObjEntity();
    entity.setName("mock");
    entity.setDataMap(dataMap);
    entity.setDbEntity(mockDbEntity);
    ObjAttribute attribute = new ObjAttribute();
    attribute.setName("not_name");
    attribute.setDbAttributePath("name");
    attribute.setType("my.type");
    entity.addAttribute(attribute);
    dataMap.addObjEntity(entity);
    EntityResolver resolver = new EntityResolver();
    resolver.addDataMap(dataMap);
    BaseProperty<?> property0 = PropertyFactory.createBase(ExpressionFactory.dbPathExp("name"), String.class);
    Collection<Property<?>> properties = Collections.singleton(property0);
    CustomColumnSetExtractor extractor = new CustomColumnSetExtractor(context, properties);
    extractor.extract();
    assertEquals(1, context.getResultNodeList().size());
    ResultNodeDescriptor descriptor0 = context.getResultNodeList().get(0);
    assertSame(property0, descriptor0.getProperty());
    assertNotNull(descriptor0.getNode());
    assertThat(descriptor0.getNode(), instanceOf(ColumnNode.class));
    assertFalse(descriptor0.isAggregate());
    assertTrue(descriptor0.isInDataRow());
    assertNotNull(descriptor0.getDbAttribute());
    assertNull(descriptor0.getDataRowKey());
    assertEquals(Types.VARBINARY, descriptor0.getJdbcType());
    assertEquals("java.lang.String", descriptor0.getJavaType());
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) BaseProperty(org.apache.cayenne.exp.property.BaseProperty) Property(org.apache.cayenne.exp.property.Property) Test(org.junit.Test)

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