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(", ");
}
}
}
}
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();
}
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);
}
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());
}
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();
}
Aggregations