@Override
public void insertOrUpdateAssociation(AssociationKey key, Association association, AssociationContext associationContext) {
DBCollection collection;
DBObject query;
MongoDBAssociationSnapshot assocSnapshot = (MongoDBAssociationSnapshot) association.getSnapshot();
String associationField;
AssociationStorageStrategy storageStrategy = getAssociationStorageStrategy( key, associationContext );
WriteConcern writeConcern = getWriteConcern( associationContext );
List<?> rows = getAssociationRows( association, key );
Object toStore = key.getMetadata().isOneToOne() ? rows.get( 0 ) : rows;
// We need to execute the previous operations first or it won't be able to find the key that should have
// been created
executeBatch( associationContext.getOperationsQueue() );
if ( storageStrategy == AssociationStorageStrategy.IN_ENTITY ) {
collection = this.getCollection( key.getEntityKey() );
query = this.prepareIdObject( key.getEntityKey() );
associationField = key.getMetadata().getCollectionRole();
( (MongoDBTupleSnapshot) associationContext.getEntityTuple().getSnapshot() ).getDbObject().put( key.getMetadata().getCollectionRole(), toStore );
}
else {
collection = getAssociationCollection( key, storageStrategy );
query = assocSnapshot.getQueryObject();
associationField = ROWS_FIELDNAME;
}
DBObject update = new BasicDBObject( "$set", new BasicDBObject( associationField, toStore ) );