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 blaze-persistence by Blazebit.
the class EclipseLinkJpaProvider method getJoinTable.
@Override
public JoinTable getJoinTable(EntityType<?> ownerType, String attributeName) {
DatabaseMapping mapping = getAttribute(ownerType, attributeName).getMapping();
if (mapping instanceof OneToOneMapping) {
OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
if (oneToOneMapping.hasRelationTable()) {
Map<String, String> idColumnMapping = new LinkedHashMap<>();
Map<String, String> keyMapping = null;
Map<String, String> keyColumnTypes = null;
Map<String, String> targetIdColumnMapping = new LinkedHashMap<>();
return new JoinTable(oneToOneMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping, keyColumnTypes, null, targetIdColumnMapping);
}
} else if (mapping instanceof CollectionMapping) {
CollectionMapping collectionMapping = (CollectionMapping) mapping;
if (collectionMapping instanceof ManyToManyMapping) {
ManyToManyMapping manyToManyMapping = (ManyToManyMapping) collectionMapping;
Vector<DatabaseField> sourceKeyFields = manyToManyMapping.getSourceKeyFields();
Vector<DatabaseField> sourceRelationKeyFields = manyToManyMapping.getSourceRelationKeyFields();
Vector<DatabaseField> targetKeyFields = manyToManyMapping.getTargetKeyFields();
Vector<DatabaseField> targetRelationKeyFields = manyToManyMapping.getTargetRelationKeyFields();
Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
Map<String, String> targetIdColumnMapping = new LinkedHashMap<>(targetKeyFields.size());
for (int i = 0; i < sourceKeyFields.size(); i++) {
idColumnMapping.put(sourceKeyFields.get(i).getName(), sourceRelationKeyFields.get(i).getName());
}
for (int i = 0; i < targetKeyFields.size(); i++) {
targetIdColumnMapping.put(targetKeyFields.get(i).getName(), targetRelationKeyFields.get(i).getName());
}
return new JoinTable(manyToManyMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping(manyToManyMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
} else if (collectionMapping instanceof DirectCollectionMapping) {
DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping) collectionMapping;
Vector<DatabaseField> sourceKeyFields = directCollectionMapping.getSourceKeyFields();
Vector<DatabaseField> referenceKeyFields = directCollectionMapping.getReferenceKeyFields();
Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
Map<String, String> targetIdColumnMapping = Collections.emptyMap();
for (int i = 0; i < sourceKeyFields.size(); i++) {
idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
}
return new JoinTable(directCollectionMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
} else if (collectionMapping instanceof DirectMapMapping) {
DirectMapMapping directMapMapping = (DirectMapMapping) collectionMapping;
Vector<DatabaseField> sourceKeyFields = directMapMapping.getSourceKeyFields();
Vector<DatabaseField> referenceKeyFields = directMapMapping.getReferenceKeyFields();
Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
Map<String, String> targetIdColumnMapping = Collections.emptyMap();
for (int i = 0; i < sourceKeyFields.size(); i++) {
idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
}
return new JoinTable(directMapMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directMapMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
} else if (collectionMapping instanceof AggregateCollectionMapping) {
AggregateCollectionMapping aggregateCollectionMapping = (AggregateCollectionMapping) collectionMapping;
Vector<DatabaseField> sourceKeyFields = aggregateCollectionMapping.getSourceKeyFields();
Vector<DatabaseField> targetForeignKeyFields = aggregateCollectionMapping.getTargetForeignKeyFields();
Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
Map<String, String> targetIdColumnMapping = Collections.emptyMap();
String tableName = null;
for (int i = 0; i < sourceKeyFields.size(); i++) {
tableName = targetForeignKeyFields.get(i).getTableName();
idColumnMapping.put(sourceKeyFields.get(i).getName(), targetForeignKeyFields.get(i).getName());
}
return new JoinTable(tableName, null, idColumnMapping, keyMapping(aggregateCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
}
}
return null;
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class EmployeeSystem method addDescriptors.
@Override
public void addDescriptors(DatabaseSession session) {
if (shouldOverrideContainerPolicy) {
List<CollectionMapping> listOrderMappings = ((EmployeeProject) this.project).getListOrderMappings();
for (int i = 0; i < listOrderMappings.size(); i++) {
CollectionMapping mapping = listOrderMappings.get(i);
mapping.setContainerPolicy(new NullsLastOrderedListContainerPolicy(mapping.getContainerPolicy().getContainerClass()));
}
}
session.addDescriptors(project);
}
use of org.eclipse.persistence.mappings.CollectionMapping in project eclipselink by eclipse-ee4j.
the class ProjectXMLOrderByQueryKeysTest method test.
@Override
public void test() {
Project project = new EmployeeProject();
CollectionMapping mapping = (CollectionMapping) project.getDescriptor(Employee.class).getMappingForAttributeName(ATTRIBUTE_NAME);
// Add the new order by query keys to the mapping
mapping.addAscendingOrdering("ascending1");
mapping.addDescendingOrdering("descending1");
mapping.addAscendingOrdering("ascending2");
mapping.addDescendingOrdering("descending2");
m_queryKeyExpressionsBeforeWrite = mapping.getOrderByQueryKeyExpressions();
// Write out the project with changes and read back in again.
XMLProjectWriter.write(TEMP_FILE, project);
project = XMLProjectReader.read(TEMP_FILE, getClass().getClassLoader());
mapping = (CollectionMapping) project.getDescriptor(Employee.class).getMappingForAttributeName(ATTRIBUTE_NAME);
// Store the query keys after reading them back in again
m_queryKeyExpressionsAfterWrite = mapping.getOrderByQueryKeyExpressions();
}
Aggregations