Search in sources :

Example 16 with OneToManyMapping

use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.

the class SQLSelectStatement method appendHierarchicalQueryClauseToWriter.

/**
 * This method will append the Hierarchical Query Clause to the end of the
 * select statement
 */
public void appendHierarchicalQueryClauseToWriter(ExpressionSQLPrinter printer) throws IOException {
    Expression startWith = getStartWithExpression();
    Expression connectBy = getConnectByExpression();
    List<Expression> orderSiblingsBy = getOrderSiblingsByExpressions();
    // Create the START WITH CLAUSE
    if (startWith != null) {
        printer.getWriter().write(" START WITH ");
        startWith.printSQL(printer);
    }
    if (connectBy != null) {
        if (!connectBy.isQueryKeyExpression()) {
            throw QueryException.illFormedExpression(connectBy);
        }
        printer.getWriter().write(" CONNECT BY ");
        DatabaseMapping mapping = ((QueryKeyExpression) connectBy).getMapping();
        ClassDescriptor descriptor = mapping.getDescriptor();
        // only works for these kinds of mappings. The data isn't hierarchical otherwise
        // Should also check that the source class and target class are the same.
        Map<DatabaseField, DatabaseField> foreignKeys = null;
        if (mapping.isOneToManyMapping()) {
            OneToManyMapping otm = (OneToManyMapping) mapping;
            foreignKeys = otm.getTargetForeignKeysToSourceKeys();
        } else if (mapping.isOneToOneMapping()) {
            OneToOneMapping oto = (OneToOneMapping) mapping;
            foreignKeys = oto.getSourceToTargetKeyFields();
        } else if (mapping.isAggregateCollectionMapping()) {
            AggregateCollectionMapping acm = (AggregateCollectionMapping) mapping;
            foreignKeys = acm.getTargetForeignKeyToSourceKeys();
        } else {
            throw QueryException.invalidQueryKeyInExpression(connectBy);
        }
        DatabaseTable defaultTable = descriptor.getDefaultTable();
        String tableName = "";
        // determine which table name to use
        if (requiresAliases()) {
            tableName = getBuilder().aliasForTable(defaultTable).getName();
        } else {
            tableName = defaultTable.getNameDelimited(printer.getPlatform());
        }
        if ((foreignKeys != null) && !foreignKeys.isEmpty()) {
            // get the source and target fields.
            Iterator<DatabaseField> sourceKeys = foreignKeys.keySet().iterator();
            // only one, use the simplest version without ugly bracets
            if (foreignKeys.size() > 1) {
                printer.getWriter().write("((");
            }
            DatabaseField source = sourceKeys.next();
            DatabaseField target = foreignKeys.get(source);
            ReadAllQuery.Direction direction = getDirection() != null ? getDirection() : ReadAllQuery.Direction.getDefault(mapping);
            if (direction == CHILD_TO_PARENT) {
                printer.getWriter().write("PRIOR " + tableName + "." + source.getNameDelimited(printer.getPlatform()));
                printer.getWriter().write(" = " + tableName + "." + target.getNameDelimited(printer.getPlatform()));
            } else {
                printer.getWriter().write(tableName + "." + source.getNameDelimited(printer.getPlatform()));
                printer.getWriter().write(" = PRIOR " + tableName + "." + target.getNameDelimited(printer.getPlatform()));
            }
            while (sourceKeys.hasNext()) {
                printer.getWriter().write(") AND (");
                source = sourceKeys.next();
                target = foreignKeys.get(source);
                if (direction == CHILD_TO_PARENT) {
                    printer.getWriter().write("PRIOR " + tableName + "." + source.getNameDelimited(printer.getPlatform()));
                    printer.getWriter().write(" = " + tableName + "." + target.getNameDelimited(printer.getPlatform()));
                } else {
                    printer.getWriter().write(tableName + "." + source.getNameDelimited(printer.getPlatform()));
                    printer.getWriter().write(" = PRIOR " + tableName + "." + target.getNameDelimited(printer.getPlatform()));
                }
            }
            if (foreignKeys.size() > 1) {
                printer.getWriter().write("))");
            }
        }
    }
    if ((orderSiblingsBy != null) && !orderSiblingsBy.isEmpty()) {
        printer.getWriter().write(" ORDER SIBLINGS BY ");
        for (Iterator<Expression> iterator = orderSiblingsBy.iterator(); iterator.hasNext(); ) {
            Expression expression = iterator.next();
            expression.printSQL(printer);
            if (iterator.hasNext()) {
                printer.getWriter().write(", ");
            }
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DecoratedDatabaseTable(org.eclipse.persistence.internal.history.DecoratedDatabaseTable) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 17 with OneToManyMapping

use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.

the class ParameterAndMappingWithTransparentIndirectionMismatchTest method setup.

@Override
protected void setup() {
    expectedException = DescriptorException.parameterAndMappingWithTransparentIndirectionMismatch(new OneToManyMapping(), null, null);
    orgIntegrityChecker = getSession().getIntegrityChecker();
    getSession().setIntegrityChecker(new IntegrityChecker());
    getSession().getIntegrityChecker().dontCatchExceptions();
}
Also used : IntegrityChecker(org.eclipse.persistence.exceptions.IntegrityChecker) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping)

Example 18 with OneToManyMapping

use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.

the class ForeignKeyTestSuite method testMapKeyForeignKey.

/**
 * Tests a map key foreign key setting.
 */
public void testMapKeyForeignKey() {
    ClassDescriptor runnerDescriptor = getPersistenceUnitServerSession().getDescriptor(Runner.class);
    OneToManyMapping mapping = (OneToManyMapping) runnerDescriptor.getMappingForAttributeName("shoes");
    OneToOneMapping keyMapping = (OneToOneMapping) ((MappedKeyMapContainerPolicy) mapping.getContainerPolicy()).getKeyMapping();
    DatabaseTable table = keyMapping.getForeignKeyFields().get(0).getTable();
    assertForeignKeyConstraint("Runner_ShoeTag_Foreign_Key", "FOREIGN KEY (TAG_ID) REFERENCES JPA21_DDL_SHOE_TAG (ID)", table);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 19 with OneToManyMapping

use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.

the class SimpleTypes_OneToMany method verifyConfig.

@Test
public void verifyConfig() throws Exception {
    ClassDescriptor descriptorA = helper.getSession().getClassDescriptorForAlias("SimpleA");
    assertNotNull("No descriptor found for alias='SimpleA'", descriptorA);
    DynamicType simpleTypeA = helper.getType("SimpleA");
    assertNotNull("'SimpleA' EntityType not found", simpleTypeA);
    assertEquals(descriptorA, simpleTypeA.getDescriptor());
    DirectToFieldMapping a_id = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("id");
    assertEquals(int.class, a_id.getAttributeClassification());
    DirectToFieldMapping a_value1 = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("value1");
    assertEquals(String.class, a_value1.getAttributeClassification());
    ClassDescriptor descriptorB = helper.getSession().getClassDescriptorForAlias("SimpleB");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicType simpleTypeB = helper.getType("SimpleB");
    assertNotNull("'SimpleB' EntityType not found", simpleTypeB);
    assertEquals(descriptorB, simpleTypeB.getDescriptor());
    DirectToFieldMapping b_id = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("id");
    assertEquals(int.class, b_id.getAttributeClassification());
    DirectToFieldMapping b_value1 = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("value1");
    assertEquals(String.class, b_value1.getAttributeClassification());
    OneToManyMapping a_b = (OneToManyMapping) descriptorA.getMappingForAttributeName("b");
    assertEquals(descriptorB, a_b.getReferenceDescriptor());
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Example 20 with OneToManyMapping

use of org.eclipse.persistence.mappings.OneToManyMapping in project eclipselink by eclipse-ee4j.

the class SimpleTypes_OneToMany method removeAwithB_PrivateOwned.

@Test
public void removeAwithB_PrivateOwned() {
    createAwithB();
    DynamicType simpleAType = helper.getType("SimpleA");
    ((OneToManyMapping) simpleAType.getDescriptor().getMappingForAttributeName("b")).setIsPrivateOwned(true);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    DynamicEntity a = em.find(simpleAType.getJavaClass(), 1);
    assertNotNull(a);
    assertEquals(1, a.<Collection>get("b").size());
    em.remove(a);
    // em.remove(a.get("b", List.class).get(0));
    em.getTransaction().commit();
}
Also used : EntityManager(jakarta.persistence.EntityManager) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) OneToManyMapping(org.eclipse.persistence.mappings.OneToManyMapping) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

OneToManyMapping (org.eclipse.persistence.mappings.OneToManyMapping)50 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)18 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)18 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)16 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)13 Expression (org.eclipse.persistence.expressions.Expression)6 TransparentIndirectionPolicy (org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy)6 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)6 UnidirectionalOneToManyMapping (org.eclipse.persistence.mappings.UnidirectionalOneToManyMapping)6 Test (org.junit.Test)6 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)5 TransformationMapping (org.eclipse.persistence.mappings.TransformationMapping)5 DynamicType (org.eclipse.persistence.dynamic.DynamicType)4 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)4 ManyToManyMapping (org.eclipse.persistence.mappings.ManyToManyMapping)4 EntityManager (jakarta.persistence.EntityManager)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)3