use of org.eclipse.persistence.mappings.AggregateCollectionMapping 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.AggregateCollectionMapping in project eclipselink by eclipse-ee4j.
the class NoSubClassMatchTest_AggregateCollection method test.
@Override
public void test() {
((DatabaseSession) getSession()).addDescriptor(descriptor());
AggregateCollectionMapping mapping = new AggregateCollectionMapping();
mapping.setReferenceClass(org.eclipse.persistence.testing.tests.validation.EmployeeWithProblems.class);
mapping.setSelectionSQLString("SELECT * FROM TABLE");
mapping.initialize((AbstractSession) getSession());
try {
mapping.getReferenceDescriptor(NoSubClassMatchTest_AggregateCollection.class, (AbstractSession) getSession());
} catch (EclipseLinkException exception) {
caughtException = exception;
}
}
use of org.eclipse.persistence.mappings.AggregateCollectionMapping in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject method buildAggregateCollectionMappingDescriptor.
protected ClassDescriptor buildAggregateCollectionMappingDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(AggregateCollectionMapping.class);
descriptor.getInheritancePolicy().setParentClass(CollectionMapping.class);
XMLCompositeCollectionMapping sourceToTargetKeyFieldAssociationsMapping = new XMLCompositeCollectionMapping();
sourceToTargetKeyFieldAssociationsMapping.setReferenceClass(Association.class);
// Handle translation of foreign key associations to hashtables.
sourceToTargetKeyFieldAssociationsMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
List<DatabaseField> sourceFields = mapping.getSourceKeyFields();
List<DatabaseField> targetFields = mapping.getTargetForeignKeyFields();
List<Association> associations = new ArrayList<>(sourceFields.size());
for (int index = 0; index < sourceFields.size(); index++) {
associations.add(new Association(targetFields.get(index), sourceFields.get(index)));
}
return associations;
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
AggregateCollectionMapping mapping = (AggregateCollectionMapping) object;
@SuppressWarnings({ "unchecked" }) List<Association> associations = (List<Association>) value;
mapping.setSourceKeyFields(NonSynchronizedVector.newInstance(associations.size()));
mapping.setTargetForeignKeyFields(NonSynchronizedVector.newInstance(associations.size()));
Iterator<Association> iterator = associations.iterator();
while (iterator.hasNext()) {
Association association = iterator.next();
mapping.getSourceKeyFields().add((DatabaseField) association.getValue());
mapping.getTargetForeignKeyFields().add((DatabaseField) association.getKey());
}
}
});
sourceToTargetKeyFieldAssociationsMapping.setAttributeName("sourceToTargetKeyFieldAssociations");
sourceToTargetKeyFieldAssociationsMapping.setXPath(getPrimaryNamespaceXPath() + "target-foreign-key/" + getSecondaryNamespaceXPath() + "field-reference");
descriptor.addMapping(sourceToTargetKeyFieldAssociationsMapping);
XMLDirectMapping relationshipPartnerAttributeNameMapping = new XMLDirectMapping();
relationshipPartnerAttributeNameMapping.setAttributeName("relationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setGetMethodName("getRelationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setSetMethodName("setRelationshipPartnerAttributeName");
relationshipPartnerAttributeNameMapping.setXPath(getPrimaryNamespaceXPath() + "bidirectional-target-attribute/text()");
descriptor.addMapping(relationshipPartnerAttributeNameMapping);
XMLDirectMapping usesBatchReadingMapping = new XMLDirectMapping();
usesBatchReadingMapping.setAttributeName("usesBatchReading");
usesBatchReadingMapping.setGetMethodName("shouldUseBatchReading");
usesBatchReadingMapping.setSetMethodName("setUsesBatchReading");
usesBatchReadingMapping.setXPath(getPrimaryNamespaceXPath() + "batch-reading/text()");
usesBatchReadingMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(usesBatchReadingMapping);
XMLCompositeObjectMapping containerPolicyMapping = new XMLCompositeObjectMapping();
containerPolicyMapping.setAttributeName("collectionPolicy");
containerPolicyMapping.setGetMethodName("getContainerPolicy");
containerPolicyMapping.setSetMethodName("setContainerPolicy");
containerPolicyMapping.setReferenceClass(ContainerPolicy.class);
containerPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "container");
descriptor.addMapping(containerPolicyMapping);
XMLCompositeObjectMapping indirectionPolicyMapping = new XMLCompositeObjectMapping();
indirectionPolicyMapping.setReferenceClass(IndirectionPolicy.class);
// Handle translation of NoIndirectionPolicy -> null.
indirectionPolicyMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
IndirectionPolicy policy = ((ForeignReferenceMapping) object).getIndirectionPolicy();
if (policy instanceof NoIndirectionPolicy) {
return null;
}
return policy;
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
IndirectionPolicy policy = (IndirectionPolicy) value;
if (value == null) {
policy = new NoIndirectionPolicy();
}
((ForeignReferenceMapping) object).setIndirectionPolicy(policy);
}
});
indirectionPolicyMapping.setAttributeName("indirectionPolicy");
indirectionPolicyMapping.setXPath(getPrimaryNamespaceXPath() + "indirection");
descriptor.addMapping(indirectionPolicyMapping);
XMLCompositeObjectMapping selectionQueryMapping = new XMLCompositeObjectMapping();
selectionQueryMapping.setAttributeName("selectionQuery");
selectionQueryMapping.setGetMethodName("getSelectionQuery");
selectionQueryMapping.setSetMethodName("setSelectionQuery");
selectionQueryMapping.setReferenceClass(ReadQuery.class);
selectionQueryMapping.setXPath(getPrimaryNamespaceXPath() + "selection-query");
descriptor.addMapping(selectionQueryMapping);
// delete-all query
return descriptor;
}
use of org.eclipse.persistence.mappings.AggregateCollectionMapping in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappings.
// Create SQLDeleteAllStatements for mappings that may be responsible for references
// to the objects to be deleted
// in the tables NOT mapped to any class: ManyToManyMapping and DirectCollectionMapping
/**
* NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappingsWithTempTable:
* if you are updating this method consider applying a similar update to that method as well.
*
* @return {@code Vector<SQLDeleteAllStatement>}
*/
protected Vector buildDeleteAllStatementsForMappings(SQLCall selectCallForExist, SQLSelectStatement selectStatementForExist, boolean dontCheckDescriptor) {
Vector deleteStatements = new Vector();
ClassDescriptor descriptor = getDescriptor();
for (DatabaseMapping mapping : descriptor.getMappings()) {
if (mapping.isForeignReferenceMapping()) {
Vector sourceFields = null;
Vector targetFields = null;
if (mapping.isDirectCollectionMapping()) {
if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
}
} else if (mapping.isAggregateCollectionMapping()) {
if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
}
} else if (mapping.isManyToManyMapping()) {
if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
sourceFields = relationTableMechanism.getSourceKeyFields();
targetFields = relationTableMechanism.getSourceRelationKeyFields();
}
} else if (mapping.isOneToOneMapping()) {
RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
if (relationTableMechanism != null) {
if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = relationTableMechanism.getSourceKeyFields();
targetFields = relationTableMechanism.getSourceRelationKeyFields();
}
}
}
if (sourceFields != null) {
deleteStatements.add(buildDeleteAllStatementForMapping(selectCallForExist, selectStatementForExist, sourceFields, targetFields));
}
}
}
return deleteStatements;
}
use of org.eclipse.persistence.mappings.AggregateCollectionMapping in project eclipselink by eclipse-ee4j.
the class ExpressionQueryMechanism method buildDeleteAllStatementsForMappingsWithTempTable.
/**
* Build delete statements with temporary table for ManyToMany and DirectCollection mappings.
*
* NOTE: A similar pattern also used in method buildDeleteAllStatementsForMappings():
* if you are updating this method consider applying a similar update to that method as well.
*
* @return {@code Vector<SQLDeleteAllStatementForTempTable>}
*/
protected Vector buildDeleteAllStatementsForMappingsWithTempTable(ClassDescriptor descriptor, DatabaseTable rootTable, boolean dontCheckDescriptor) {
Vector deleteStatements = new Vector();
for (DatabaseMapping mapping : descriptor.getMappings()) {
if (mapping.isForeignReferenceMapping()) {
List<DatabaseField> sourceFields = null;
List<DatabaseField> targetFields = null;
if (mapping.isDirectCollectionMapping()) {
if (shouldBuildDeleteStatementForMapping((DirectCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = ((DirectCollectionMapping) mapping).getSourceKeyFields();
targetFields = ((DirectCollectionMapping) mapping).getReferenceKeyFields();
}
} else if (mapping.isAggregateCollectionMapping()) {
if (shouldBuildDeleteStatementForMapping((AggregateCollectionMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = ((AggregateCollectionMapping) mapping).getSourceKeyFields();
targetFields = ((AggregateCollectionMapping) mapping).getTargetForeignKeyFields();
}
} else if (mapping.isManyToManyMapping()) {
if (shouldBuildDeleteStatementForMapping((ManyToManyMapping) mapping, dontCheckDescriptor, descriptor)) {
RelationTableMechanism relationTableMechanism = ((ManyToManyMapping) mapping).getRelationTableMechanism();
sourceFields = relationTableMechanism.getSourceKeyFields();
targetFields = relationTableMechanism.getSourceRelationKeyFields();
}
} else if (mapping.isOneToOneMapping()) {
RelationTableMechanism relationTableMechanism = ((OneToOneMapping) mapping).getRelationTableMechanism();
if (relationTableMechanism != null) {
if (shouldBuildDeleteStatementForMapping((OneToOneMapping) mapping, dontCheckDescriptor, descriptor)) {
sourceFields = relationTableMechanism.getSourceKeyFields();
targetFields = relationTableMechanism.getSourceRelationKeyFields();
}
}
}
if (sourceFields != null) {
DatabaseTable targetTable = targetFields.get(0).getTable();
SQLDeleteAllStatementForTempTable deleteStatement = buildDeleteAllStatementForTempTable(rootTable, sourceFields, targetTable, targetFields);
deleteStatements.addElement(deleteStatement);
}
}
}
return deleteStatements;
}
Aggregations