use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class QueryKeyExpression method getLeafMapping.
/**
* INTERNAL:
* Lookup the mapping for this item by traversing its expression recursively.
* If an aggregate of foreign mapping is found it is traversed.
*/
@Override
public DatabaseMapping getLeafMapping(DatabaseQuery query, ClassDescriptor rootDescriptor, AbstractSession session) {
if (isMapEntryExpression()) {
MapEntryExpression mapEntryExpression = (MapEntryExpression) this;
// get the expression that we want the table entry for
QueryKeyExpression baseExpression = (QueryKeyExpression) mapEntryExpression.getBaseExpression();
// get the expression that owns the mapping for the table entry
Expression owningExpression = baseExpression.getBaseExpression();
ClassDescriptor owningDescriptor = owningExpression.getLeafDescriptor(query, rootDescriptor, session);
// Get the mapping that owns the table
CollectionMapping mapping = (CollectionMapping) owningDescriptor.getObjectBuilder().getMappingForAttributeName(baseExpression.getName());
if (mapEntryExpression.shouldReturnMapEntry()) {
return mapping;
}
if (mapping.getContainerPolicy().isMappedKeyMapPolicy()) {
MappedKeyMapContainerPolicy policy = (MappedKeyMapContainerPolicy) mapping.getContainerPolicy();
return (DatabaseMapping) policy.getKeyMapping();
}
return mapping;
}
Expression baseExpression = getBaseExpression();
ClassDescriptor descriptor = baseExpression.getLeafDescriptor(query, rootDescriptor, session);
if (descriptor == null) {
return null;
}
return descriptor.getObjectBuilder().getMappingForAttributeName(getName());
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class ProjectClassGenerator method addForeignReferenceMappingLines.
protected void addForeignReferenceMappingLines(NonreflectiveMethodDefinition method, String mappingName, ForeignReferenceMapping mapping) {
if (mapping.getReferenceClassName() != null) {
method.addLine(mappingName + ".setReferenceClass(" + mapping.getReferenceClassName() + ".class);");
}
if (mapping.getRelationshipPartnerAttributeName() != null) {
method.addLine(mappingName + ".setRelationshipPartnerAttributeName(\"" + mapping.getRelationshipPartnerAttributeName() + "\");");
}
IndirectionPolicy policy = mapping.getIndirectionPolicy();
if (policy instanceof ContainerIndirectionPolicy) {
String containerClassName = ((ContainerIndirectionPolicy) policy).getContainerClassName();
method.addLine(mappingName + ".useContainerIndirection(" + containerClassName + ".class);");
// Bug#4251902 used in ObjectReferenceMapping
} else if (policy instanceof ProxyIndirectionPolicy) {
method.addLine(mappingName + ".useProxyIndirection();");
} else if (policy instanceof BasicIndirectionPolicy) {
method.addLine(mappingName + ".useBasicIndirection();");
} else if (policy instanceof NoIndirectionPolicy) {
method.addLine(mappingName + ".dontUseIndirection();");
}
if (mapping.shouldUseBatchReading()) {
method.addLine(mappingName + ".useBatchReading();");
}
if (mapping.isJoinFetched()) {
if (mapping.isInnerJoinFetched()) {
method.addLine(mappingName + ".useInnerJoinFetch();");
} else if (mapping.isOuterJoinFetched()) {
method.addLine(mappingName + ".useOuterJoinFetch();");
}
}
if ((!mapping.isDirectCollectionMapping()) && mapping.isPrivateOwned()) {
method.addLine(mappingName + ".privateOwnedRelationship();");
}
if (mapping.isCollectionMapping()) {
CollectionMapping collectionMapping = (CollectionMapping) mapping;
String collectionClassName = collectionMapping.getContainerPolicy().getContainerClassName();
if (mapping.getContainerPolicy().isCollectionPolicy()) {
if (policy instanceof TransparentIndirectionPolicy) {
method.addLine(mappingName + ".useTransparentCollection();");
}
if (!collectionClassName.equals(Vector.class.getName())) {
method.addLine(mappingName + ".useCollectionClass(" + collectionClassName + ".class);");
}
} else if (collectionMapping.isDirectMapMapping()) {
if (policy instanceof TransparentIndirectionPolicy) {
method.addLine(mappingName + ".useTransparentMap();");
if (!collectionClassName.equals(IndirectMap.class.getName())) {
method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
}
} else {
method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class);");
}
} else if (collectionMapping.getContainerPolicy().isMapPolicy()) {
String keyMethodName = ((org.eclipse.persistence.internal.queries.MapContainerPolicy) collectionMapping.getContainerPolicy()).getKeyName();
if (policy instanceof TransparentIndirectionPolicy) {
method.addLine(mappingName + ".useTransparentMap(\"" + keyMethodName + "\");");
if (!collectionClassName.equals(IndirectMap.class.getName())) {
method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
}
} else {
method.addLine(mappingName + ".useMapClass(" + collectionClassName + ".class, \"" + keyMethodName + "\");");
}
}
// Ordering.
Iterator<Expression> queryKeyExpressions = collectionMapping.getOrderByQueryKeyExpressions().iterator();
while (queryKeyExpressions.hasNext()) {
FunctionExpression expression = (FunctionExpression) queryKeyExpressions.next();
String queryKeyName = expression.getBaseExpression().getName();
if (expression.getOperator().getSelector() == ExpressionOperator.Descending) {
method.addLine(mappingName + ".addDescendingOrdering(\"" + queryKeyName + "\");");
} else {
method.addLine(mappingName + ".addAscendingOrdering(\"" + queryKeyName + "\");");
}
}
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestCase method testLazyListInstantiationEager.
// bug 325035
public void testLazyListInstantiationEager() {
EntityManager em = createEntityManager();
CollectionMapping mapping = ((CollectionMapping) getServerSession().getProject().getClassDescriptor(Company.class).getMappingForAttributeName("vehicles"));
Boolean lazyIndirection = mapping.shouldUseLazyInstantiationForIndirectCollection();
mapping.setUseLazyInstantiationForIndirectCollection(false);
beginTransaction(em);
Company company = new Company();
company.setName("ListCo");
em.persist(company);
Car car = new Car();
em.persist(car);
company.getVehicles().add(car);
car.setOwner(company);
commitTransaction(em);
clearCache();
try {
em = createEntityManager();
company = em.find(Company.class, company.getId());
company.getVehicles().add(new Car());
assertTrue("Lazy instantiation was not disabled for IndirectList.", ((IndirectList) company.getVehicles()).getAddedElements().size() == 0);
} finally {
em = createEntityManager();
beginTransaction(em);
company = em.find(Company.class, company.getId());
car = em.find(Car.class, car.getId());
em.remove(company);
em.remove(car);
commitTransaction(em);
mapping.setUseLazyInstantiationForIndirectCollection(lazyIndirection);
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class ManyToManyAccessor method process.
/**
* INTERNAL:
* Process a many to many metadata accessor into a EclipseLink
* ManyToManyMapping.
*/
@Override
public void process() {
super.process();
// Create a M-M mapping and process common collection mapping metadata.
// Allow for different descriptor types (EIS) to create different mapping types.
CollectionMapping mapping = getDescriptor().getClassDescriptor().newManyToManyMapping();
process(mapping);
if (hasMappedBy()) {
// We are processing the non-owning side of a M-M relationship. Get
// the owning mapping from the reference descriptor metadata and
// process the keys from it.
DatabaseMapping owningMapping = getOwningMapping();
if (owningMapping.isManyToManyMapping()) {
ManyToManyMapping ownerMapping = (ManyToManyMapping) owningMapping;
processMappedByRelationTable(ownerMapping.getRelationTableMechanism(), ((ManyToManyMapping) mapping).getRelationTableMechanism());
// Set the mapping read-only.
mapping.setIsReadOnly(true);
} else {
// Invalid owning mapping type, throw an exception.
throw ValidationException.invalidMapping(getJavaClass(), getReferenceClass());
}
} else if (mapping instanceof ManyToManyMapping) {
// Processing the owning side of a M-M, process the join table.
processJoinTable(mapping, ((ManyToManyMapping) mapping).getRelationTableMechanism(), getJoinTableMetadata());
} else if (mapping instanceof EISOneToManyMapping) {
processEISOneToManyMapping((EISOneToManyMapping) mapping);
}
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class MappingAccessor method setIndirectionPolicy.
/**
* INTERNAL:
* Set the correct indirection policy on a collection mapping. Method
* assume that the reference class has been set on the mapping before
* calling this method.
*/
protected void setIndirectionPolicy(ContainerMapping mapping, String mapKey, boolean usesIndirection) {
MetadataClass rawClass = getRawClass();
boolean containerPolicySet = false;
if (usesIndirection && (mapping instanceof ForeignReferenceMapping)) {
containerPolicySet = true;
CollectionMapping collectionMapping = (CollectionMapping) mapping;
if (rawClass.isClass(Map.class)) {
if (collectionMapping.isDirectMapMapping()) {
((DirectMapMapping) mapping).useTransparentMap();
} else {
collectionMapping.useTransparentMap(mapKey);
}
} else if (rawClass.isClass(List.class)) {
collectionMapping.useTransparentList();
} else if (rawClass.isClass(Collection.class)) {
collectionMapping.useTransparentCollection();
} else if (rawClass.isClass(Set.class)) {
collectionMapping.useTransparentSet();
} else {
getLogger().logWarningMessage(MetadataLogger.WARNING_INVALID_COLLECTION_USED_ON_LAZY_RELATION, getJavaClass(), getAnnotatedElement(), rawClass);
processIndirection((ForeignReferenceMapping) mapping);
containerPolicySet = false;
}
} else {
if (mapping instanceof CollectionMapping) {
((CollectionMapping) mapping).dontUseIndirection();
}
}
if (!containerPolicySet) {
if (rawClass.isClass(Map.class)) {
if (mapping instanceof DirectMapMapping) {
((DirectMapMapping) mapping).useMapClass(java.util.Hashtable.class);
} else {
mapping.useMapClass(java.util.Hashtable.class, mapKey);
}
} else if (rawClass.isClass(Set.class)) {
// This will cause it to use a CollectionContainerPolicy type
mapping.useCollectionClass(java.util.HashSet.class);
} else if (rawClass.isClass(List.class)) {
// This will cause a ListContainerPolicy type to be used or
// OrderedListContainerPolicy if ordering is specified.
mapping.useCollectionClass(java.util.Vector.class);
} else if (rawClass.isClass(Collection.class)) {
// Force CollectionContainerPolicy type to be used with a
// collection implementation.
mapping.setContainerPolicy(new CollectionContainerPolicy(java.util.Vector.class));
} else {
// Use the supplied collection class type if its not an interface
if (mapKey == null || mapKey.equals("")) {
if (rawClass.isList()) {
mapping.useListClassName(rawClass.getName());
} else {
mapping.useCollectionClassName(rawClass.getName());
}
} else {
mapping.useMapClassName(rawClass.getName(), mapKey);
}
}
}
}
Aggregations