org.springframework.data.mapping.Association Java Examples

The following examples show how to use org.springframework.data.mapping.Association. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultNeo4jPersistentEntity.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
private void verifyDynamicAssociations() {

		Set<Class> targetEntities = new HashSet<>();
		this.doWithAssociations((Association<Neo4jPersistentProperty> association) -> {
			Neo4jPersistentProperty inverse = association.getInverse();
			if (inverse.isDynamicAssociation()) {
				Relationship relationship = inverse.findAnnotation(Relationship.class);
				Assert.state(relationship == null || relationship.type().isEmpty(),
					() ->
						"Dynamic relationships cannot be used with a fixed type. Omit @Relationship or use @Relationship(direction = "
							+ relationship.direction().name() + ") without a type in " + this.getUnderlyingClass()
							+ " on field " + inverse.getFieldName() + ".");

				Assert.state(!targetEntities.contains(inverse.getAssociationTargetType()),
					() -> this.getUnderlyingClass() + " already contains a dynamic relationship to " + inverse
						.getAssociationTargetType()
						+ ". Only one dynamic relationship between to entities is permitted."
				);
				targetEntities.add(inverse.getAssociationTargetType());
			}
		});
	}
 
Example #2
Source File: NestedRelationshipContext.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
static NestedRelationshipContext of(Association<Neo4jPersistentProperty> handler,
	PersistentPropertyAccessor<?> propertyAccessor,
	Neo4jPersistentEntity<?> neo4jPersistentEntity) {

	Neo4jPersistentProperty inverse = handler.getInverse();

	boolean inverseValueIsEmpty = propertyAccessor.getProperty(inverse) == null;
	Object value = propertyAccessor.getProperty(inverse);

	RelationshipDescription relationship = neo4jPersistentEntity
		.getRelationships().stream()
		.filter(r -> r.getFieldName().equals(inverse.getName()))
		.findFirst().get();

	// if we have a relationship with properties, the targetNodeType is the map key
	Class<?> associationTargetType = relationship.hasRelationshipProperties()
		? inverse.getComponentType()
		: inverse.getAssociationTargetType();

	return new NestedRelationshipContext(inverse, value, relationship, associationTargetType,
		inverseValueIsEmpty);
}
 
Example #3
Source File: DefaultNeo4jPersistentEntity.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<RelationshipDescription> getRelationships() {

	final List<RelationshipDescription> relationships = new ArrayList<>();
	this.doWithAssociations((Association<Neo4jPersistentProperty> association) ->
		relationships.add((RelationshipDescription) association)
	);
	return Collections.unmodifiableCollection(relationships);
}
 
Example #4
Source File: Neo4jMappingContextTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void targetTypeOfAssociationsShouldBeKnownToTheMappingContext() {

	Neo4jMappingContext schema = new Neo4jMappingContext();
	Neo4jPersistentEntity<?> bikeNodeEntity = schema.getPersistentEntity(BikeNode.class);
	bikeNodeEntity.doWithAssociations((Association<Neo4jPersistentProperty> association) ->
		assertThat(schema.getRequiredMappingFunctionFor(association.getInverse().getAssociationTargetType()))
			.isNotNull());
}
 
Example #5
Source File: Neo4jMappingContextTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldHonourTransientAnnotation() {

	Neo4jMappingContext schema = new Neo4jMappingContext();
	Neo4jPersistentEntity<?> userNodeEntity = schema.getPersistentEntity(UserNode.class);

	assertThat(userNodeEntity.getPersistentProperty("anAnnotatedTransientProperty")).isNull();

	List<String> associations = new ArrayList<>();
	userNodeEntity.doWithAssociations((Association<Neo4jPersistentProperty> a) -> {
		associations.add(a.getInverse().getFieldName());
	});

	assertThat(associations).containsOnly("bikes");
}
 
Example #6
Source File: Neo4jMappingContextTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void enumMapKeys() {

	Neo4jMappingContext schema = new Neo4jMappingContext();
	Neo4jPersistentEntity<?> enumRelNodeEntity = schema.getPersistentEntity(EnumRelNode.class);

	List<Neo4jPersistentProperty> associations = new ArrayList<>();
	enumRelNodeEntity
		.doWithAssociations((Association<Neo4jPersistentProperty> a) -> associations.add(a.getInverse()));

	assertThat(associations).hasSize(2);
}
 
Example #7
Source File: DynamoDBPersistentPropertyImpl.java    From spring-data-dynamodb with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<DynamoDBPersistentProperty> createAssociation() {
	return new Association<DynamoDBPersistentProperty>(this, null);
}
 
Example #8
Source File: DefaultNeo4jPersistentProperty.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<Neo4jPersistentProperty> createAssociation() {

	Neo4jPersistentEntity<?> obverseOwner;

	// if the target is a relationship property always take the key type from the map instead of the value type.
	if (this.hasActualTypeAnnotation(RelationshipProperties.class)) {
		obverseOwner = this.mappingContext.getPersistentEntity(this.getComponentType());
	} else {
		obverseOwner = this.mappingContext.getPersistentEntity(this.getAssociationTargetType());
	}

	Relationship outgoingRelationship = this.findAnnotation(Relationship.class);

	String type;
	if (outgoingRelationship != null && outgoingRelationship.type() != null) {
		type = outgoingRelationship.type();
	} else {
		type = deriveRelationshipType(this.getName());
	}

	Relationship.Direction direction = Relationship.Direction.OUTGOING;
	if (outgoingRelationship != null) {
		direction = outgoingRelationship.direction();
	}

	boolean dynamicAssociation = this.isDynamicAssociation();

	// Because a dynamic association is also represented as a Map, this ensures that the
	// relationship properties class will only have a value if it's not a dynamic association.
	Class<?> relationshipPropertiesClass = dynamicAssociation ? null : getMapValueType();

	// Try to determine if there is a relationship definition that expresses logically the same relationship
	// on the other end.
	Optional<RelationshipDescription> obverseRelationshipDescription = obverseOwner.getRelationships().stream()
		.filter(rel -> rel.getType().equals(type) && rel.getTarget().equals(this.getOwner()))
		.findFirst();

	DefaultRelationshipDescription relationshipDescription = new DefaultRelationshipDescription(this,
		obverseRelationshipDescription.orElse(null), type, dynamicAssociation, (NodeDescription<?>) getOwner(),
		this.getName(), obverseOwner, direction, relationshipPropertiesClass);

	// Update the previous found, if any, relationship with the newly created one as its counterpart.
	obverseRelationshipDescription
		.ifPresent(relationship -> relationship.setRelationshipObverse(relationshipDescription));

	return relationshipDescription;
}
 
Example #9
Source File: KeyValuePersistentProperty.java    From spring-data-keyvalue with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Association<P> createAssociation() {
	return new Association<>((P) this, null);
}
 
Example #10
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 4 votes vote down vote up
/**
 * Read an incoming {@link CrateDocument} into the target entity.
 *
 * @param entity the target entity.
 * @param source the document to convert.
 * @param parent an optional parent object.
 * @param <R> the entity type.
 * @return the converted entity.
 */
@SuppressWarnings("unchecked")
protected <R> R read(final CratePersistentEntity<R> entity, final CrateDocument source, final Object parent) {
	
	final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(source, spELContext);
	
    ParameterValueProvider<CratePersistentProperty> provider = getParameterProvider(entity, source, evaluator, parent);
    
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);

    R instance = instantiator.createInstance(entity, provider);
    final PersistentPropertyAccessor propertyAccessor = entity.getPropertyAccessor(instance);
    final R result = (R)propertyAccessor.getBean();
    final CratePersistentProperty idProperty = entity.getIdProperty();
    final CratePersistentProperty versionProperty = entity.getVersionProperty();
    
    if(entity.hasIdProperty()) {
    	Object idValue = getValueInternal(idProperty, source, result);
    	propertyAccessor.setProperty(idProperty, idValue);
    }
    
    if(entity.hasVersionProperty()) {
    	Object versionValue = getValueInternal(versionProperty, source, result);
    	propertyAccessor.setProperty(versionProperty, versionValue);
    }
    
    for(CratePersistentProperty property : entity.getPersistentProperties()) {
    	// skip id and version properties as they may have potentially been set above.  
		if((idProperty != null && idProperty.equals(property)) || (versionProperty != null && versionProperty.equals(property))) {
			continue;
		}
		
		if(!source.containsKey(property.getFieldName()) || entity.isConstructorArgument(property)) {
			continue;
		}
		
		propertyAccessor.setProperty(property, getValueInternal(property, source, result));
    }
    
    entity.doWithAssociations(new AssociationHandler<CratePersistentProperty>() {
    	
      @Override
      public void doWithAssociation(final Association<CratePersistentProperty> association) {	    	  
    	  CratePersistentProperty inverseProp = association.getInverse();
    	  Object obj = getValueInternal(inverseProp, source, result);
    	  propertyAccessor.setProperty(inverseProp, obj);
      }	      
    });

    return result;
  }
 
Example #11
Source File: SimpleCratePersistentProperty.java    From spring-data-crate with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<CratePersistentProperty> createAssociation() {
	throw new UnsupportedOperationException("@Reference is not supported!");
}
 
Example #12
Source File: TypicalEntityReaderBenchmark.java    From spring-data-dev-tools with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<MyPersistentProperty> createAssociation() {
	return null;
}
 
Example #13
Source File: MybatisPersistentPropertyImpl.java    From spring-data-mybatis with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<MybatisPersistentProperty> createAssociation() {
	return new Association<>(this, null);
}
 
Example #14
Source File: FirestorePersistentPropertyImpl.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<FirestorePersistentProperty> createAssociation() {
	return new Association<>(this, null);
}
 
Example #15
Source File: SpannerCompositeKeyProperty.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Association<SpannerPersistentProperty> getAssociation() {
	return null;
}
 
Example #16
Source File: SpannerPersistentPropertyImpl.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<SpannerPersistentProperty> createAssociation() {
	return new Association<>(this, null);
}
 
Example #17
Source File: DatastorePersistentPropertyImpl.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<DatastorePersistentProperty> createAssociation() {
	return new Association<>(this, null);
}
 
Example #18
Source File: BasicCosmosPersistentProperty.java    From spring-data-cosmosdb with MIT License 4 votes vote down vote up
@Override
protected Association<CosmosPersistentProperty> createAssociation() {
    return new Association<>(this, null);
}
 
Example #19
Source File: SimpleSolrPersistentProperty.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<SolrPersistentProperty> createAssociation() {
	return null;
}
 
Example #20
Source File: DefaultArangoPersistentProperty.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@Override
protected Association<ArangoPersistentProperty> createAssociation() {
	return new Association<>(this, null);
}