Search in sources :

Example 46 with DbRelationship

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

the class EOModelProcessorTest method assertOneWayRelationships.

protected void assertOneWayRelationships(DataMap map) throws Exception {
    // assert that one way relationships are loaded properly
    // - Db loaded as two-way, obj - as one-way
    ObjEntity exhibitEntity = map.getObjEntity("Exhibit");
    ObjRelationship toTypeObject = exhibitEntity.getRelationship("toExhibitType");
    DbRelationship toTypeDB = exhibitEntity.getDbEntity().getRelationship("toExhibitType");
    assertNotNull(toTypeObject);
    assertNotNull(toTypeDB);
    assertNull(toTypeObject.getReverseRelationship());
    assertNotNull(toTypeDB.getReverseRelationship());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 47 with DbRelationship

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

the class DbArcIdTest method testHashCode.

@Test
public void testHashCode() {
    DbArcId id1 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    int h1 = id1.hashCode();
    assertEquals(h1, id1.hashCode());
    assertEquals(h1, id1.hashCode());
    DbArcId id1_eq = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r1"));
    assertEquals(h1, id1_eq.hashCode());
    DbArcId id2 = new DbArcId(new ObjectId("x", "k", "v"), new DbRelationship("r2"));
    assertFalse(h1 == id2.hashCode());
    DbArcId id3 = new DbArcId(new ObjectId("y", "k", "v"), new DbRelationship("r1"));
    assertFalse(h1 == id3.hashCode());
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) Test(org.junit.Test)

Example 48 with DbRelationship

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

the class OpenBaseJoinStack method appendJoinSubtree.

@Override
protected void appendJoinSubtree(StringBuilder out, JoinTreeNode node) {
    DbRelationship relationship = node.getRelationship();
    if (relationship == null) {
        return;
    }
    DbEntity targetEntity = relationship.getTargetEntity();
    String targetAlias = node.getTargetTableAlias();
    out.append(", ").append(targetEntity.getFullyQualifiedName()).append(' ').append(targetAlias);
    for (JoinTreeNode child : node.getChildren()) {
        appendJoinSubtree(out, child);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) JoinTreeNode(org.apache.cayenne.access.translator.select.JoinTreeNode)

Example 49 with DbRelationship

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

the class Oracle8JoinStack method appendQualifierSubtree.

protected void appendQualifierSubtree(StringBuilder out, JoinTreeNode node) {
    DbRelationship relationship = node.getRelationship();
    String srcAlias = node.getSourceTableAlias();
    String targetAlias = node.getTargetTableAlias();
    List<DbJoin> joins = relationship.getJoins();
    int len = joins.size();
    for (int i = 0; i < len; i++) {
        DbJoin join = joins.get(i);
        if (i > 0) {
            out.append(" AND ");
        }
        out.append(srcAlias).append('.').append(join.getSourceName());
        switch(node.getJoinType()) {
            case INNER:
                out.append(" = ");
                break;
            case LEFT_OUTER:
                out.append(" * ");
                break;
            default:
                throw new IllegalArgumentException("Unsupported join type: " + node.getJoinType());
        }
        out.append(targetAlias).append('.').append(join.getTargetName());
    }
    for (JoinTreeNode child : node.getChildren()) {
        out.append(" AND ");
        appendQualifierSubtree(out, child);
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) JoinTreeNode(org.apache.cayenne.access.translator.select.JoinTreeNode)

Example 50 with DbRelationship

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

the class Compiler method compileEntityResultWithPrefetch.

private EntityResult compileEntityResultWithPrefetch(EntityResult compiledResult, EJBQLExpression prefetchExpression) {
    final EntityResult result = compiledResult;
    String id = prefetchExpression.getText().toLowerCase();
    ClassDescriptor descriptor = descriptorsById.get(id);
    if (descriptor == null) {
        descriptor = descriptorsById.get(prefetchExpression.getText());
    }
    final String prefix = prefetchExpression.getText().substring(prefetchExpression.getText().indexOf(".") + 1);
    final Set<String> visited = new HashSet<String>();
    PropertyVisitor visitor = new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            ObjAttribute oa = property.getAttribute();
            if (visited.add(oa.getDbAttributePath())) {
                result.addObjectField(oa.getEntity().getName(), "fetch." + prefix + "." + oa.getName(), prefix + "." + oa.getDbAttributeName());
            }
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            return true;
        }

        public boolean visitToOne(ToOneProperty property) {
            ObjRelationship rel = property.getRelationship();
            DbRelationship dbRel = rel.getDbRelationships().get(0);
            for (DbJoin join : dbRel.getJoins()) {
                DbAttribute src = join.getSource();
                if (src.isForeignKey() && visited.add(src.getName())) {
                    result.addDbField("fetch." + prefix + "." + src.getName(), prefix + "." + src.getName());
                }
            }
            return true;
        }
    };
    descriptor.visitAllProperties(visitor);
    // append id columns ... (some may have been appended already via relationships)
    for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
        if (visited.add(pkName)) {
            result.addDbField("fetch." + prefix + "." + pkName, prefix + "." + pkName);
        }
    }
    // append inheritance discriminator columns...
    for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
        if (visited.add(column.getName())) {
            result.addDbField("fetch." + prefix + "." + column.getDbAttributePath(), prefix + "." + column.getDbAttributePath());
        }
    }
    return result;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) EntityResult(org.apache.cayenne.map.EntityResult) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) HashSet(java.util.HashSet)

Aggregations

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6