org.springframework.data.mapping.PersistentProperty Java Examples

The following examples show how to use org.springframework.data.mapping.PersistentProperty. 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: ContentPropertyUtils.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static Class<?> getContentPropertyType(PersistentProperty<?> prop) {
	Class<?> contentEntityClass = null;

	// null single-valued content property
	if (!PersistentEntityUtils.isPropertyMultiValued(prop)) {
		contentEntityClass = prop.getActualType();
	}
	// null multi-valued content property
	else if (PersistentEntityUtils.isPropertyMultiValued(prop)) {
		if (prop.isArray()) {
			contentEntityClass = prop.getComponentType();
		}
		else if (prop.isCollectionLike()) {
			contentEntityClass = prop.getActualType();
		}
	}

	return contentEntityClass;
}
 
Example #2
Source File: JsonSchemaBuilder.java    From moserp with Apache License 2.0 6 votes vote down vote up
private void populateProperties(Class<?> domainType, BusinessEntity entity) {
    Map<String, EntityProperty> properties = new HashMap<>();
    final PersistentEntity<?, ?> persistentEntity = persistentEntities.getPersistentEntity(domainType);
    JacksonMetadata jacksonMetadata = new JacksonMetadata(objectMapper, domainType);
    for (BeanPropertyDefinition definition : jacksonMetadata) {
        PersistentProperty<?> persistentProperty = persistentEntity.getPersistentProperty(definition.getInternalName());
        PropertyFactoryContext context = new PropertyFactoryContext(definition, jacksonMetadata, persistentProperty);
        PropertyFactory factory = getFactoryFor(context);
        if (factory != null) {
            EntityProperty property = factory.create(context);
            properties.put(definition.getInternalName(), property);
            if(property.isRequired()) {
                entity.getRequired().add(definition.getInternalName());
            }
        }
    }
    entity.setProperties(properties);
}
 
Example #3
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private List<Entity> getDescendantEntitiesForSave(Object entity, Key key, Set<Key> persistedEntities) {
	DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext
			.getPersistentEntity(entity.getClass());
	List<Entity> entitiesToSave = new ArrayList<>();
	datastorePersistentEntity.doWithDescendantProperties(
			(PersistentProperty persistentProperty) -> {
				//Convert and write descendants, applying ancestor from parent entry
				PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
				Object val = accessor.getProperty(persistentProperty);
				if (val != null) {
					//we can be sure that the property is an array or an iterable,
					//because we check it in isDescendant
					entitiesToSave
							.addAll(getEntitiesForSave((Iterable<?>) ValueUtil.toListIfArray(val), persistedEntities, key));
				}
			});
	return entitiesToSave;
}
 
Example #4
Source File: MappingAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link MappingAuditingMetadata} instance from the given
 * {@link PersistentEntity}.
 */
public <P> MappingAuditingMetadata(
		MappingContext<?, ? extends PersistentProperty<?>> context,
		Class<?> type) {

	Assert.notNull(type, "Type must not be null!");

	this.createdByPaths = context.findPersistentPropertyPaths(type,
			withAnnotation(CreatedBy.class,
					org.springframework.data.mybatis.annotation.CreatedBy.class));
	this.createdDatePaths = context.findPersistentPropertyPaths(type,
			withAnnotation(CreatedDate.class,
					org.springframework.data.mybatis.annotation.CreatedDate.class));
	this.lastModifiedByPaths = context.findPersistentPropertyPaths(type,
			withAnnotation(LastModifiedBy.class,
					org.springframework.data.mybatis.annotation.LastModifiedBy.class));
	this.lastModifiedDatePaths = context.findPersistentPropertyPaths(type,
			withAnnotation(LastModifiedDate.class,
					org.springframework.data.mybatis.annotation.LastModifiedDate.class));

	this.isAuditable = Lazy.of( //
			() -> Arrays
					.asList(createdByPaths, createdDatePaths, lastModifiedByPaths,
							lastModifiedDatePaths) //
					.stream() //
					.anyMatch(it -> !it.isEmpty())//
	);
}
 
Example #5
Source File: ApplicationStructureBuilderTest.java    From moserp with Apache License 2.0 5 votes vote down vote up
public void setupMocks() throws URISyntaxException {
    configuration = mock(RepositoryRestConfiguration.class);
    when(configuration.getBaseUri()).thenReturn(new URI("http://localhost:8080/"));
    mappings = mock(ResourceMappings.class);
    metaData = mock(ResourceMetadata.class);
    when(metaData.getPath()).thenReturn(new Path("valueLists"));
    PersistentEntity persistentEntity = mock(PersistentEntity.class);
    when(persistentEntities.getPersistentEntity(any())).thenReturn(persistentEntity);
    PersistentProperty persistentProperty = mock(PersistentProperty.class);
    when(persistentEntity.getPersistentProperty(any(String.class))).thenReturn(persistentProperty);
    when(entityLinks.linkFor(any())).thenReturn(BaseUriLinkBuilder.create(new URI("http://localhost:8080/")));
    moduleRegistry = mock(ModuleRegistry.class);
    when(moduleRegistry.getBaseUriForResource(anyString())).thenReturn(new RestUri("http://localhost:8080/valueLists"));
}
 
Example #6
Source File: ContentPropertyRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@StoreType("contentstore")
@RequestMapping(value = BASE_MAPPING, method = RequestMethod.DELETE)
@ResponseBody
public ResponseEntity<?> deleteContent(@RequestHeader HttpHeaders headers,
									   @PathVariable String repository,
									   @PathVariable String id,
									   @PathVariable String contentProperty,
									   @PathVariable String contentId)
		throws HttpRequestMethodNotSupportedException {

	Object domainObj = findOne(repositories, repository, id);

	String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
	Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
	HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);

	PersistentProperty<?> property = this.getContentPropertyDefinition(
			repositories.getPersistentEntity(domainObj.getClass()), contentProperty);

	Object contentPropertyValue = getContentProperty(domainObj, property, contentId);

	Class<?> contentEntityClass = ContentPropertyUtils.getContentPropertyType(property);

	ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, contentEntityClass);

	info.getImpementation().unsetContent(contentPropertyValue);

	// remove the content property reference from the data object
	// setContentProperty(domainObj, property, contentId, null);

	save(repositories, domainObj);

	return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
 
Example #7
Source File: ContentPropertyRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
private void replaceContentInternal(HttpHeaders headers, Repositories repositories,
		ContentStoreService stores, String repository, String id,
		String contentProperty, String contentId, String mimeType,
		String originalFileName, InputStream stream)
		throws HttpRequestMethodNotSupportedException {

	Object domainObj = findOne(repositories, repository, id);

	String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
	Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
	HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);

	PersistentProperty<?> property = this.getContentPropertyDefinition(repositories.getPersistentEntity(domainObj.getClass()), contentProperty);

	Object contentPropertyValue = this.getContentProperty(domainObj, property, contentId);

	if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue, MimeType.class)) {
		BeanUtils.setFieldWithAnnotation(contentPropertyValue, MimeType.class,
				mimeType);
	}

	if (originalFileName != null && StringUtils.hasText(originalFileName)) {
		if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue,
				OriginalFileName.class)) {
			BeanUtils.setFieldWithAnnotation(contentPropertyValue,
					OriginalFileName.class, originalFileName);
		}
	}

	Class<?> contentEntityClass = ContentPropertyUtils.getContentPropertyType(property);

	ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, contentEntityClass);

	info.getImpementation().setContent(contentPropertyValue, stream);

	save(repositories, domainObj);
}
 
Example #8
Source File: MybatisPersistentEntityImpl.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
public PersistentEntity<?, ? extends PersistentProperty<?>> getRequiredPersistentEntity(
		Class<?> type) {

	Assert.notNull(type, "Domain type must not be null!");

	return getPersistentEntity(type).orElseThrow(() -> new IllegalArgumentException(
			String.format("Couldn't find PersistentEntity for type %s!", type)));
}
 
Example #9
Source File: MappingAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
private <S, P extends PersistentProperty<?>> S setProperty(
		PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths,
		S value) {

	paths.forEach(it -> this.accessor.setProperty(it, value));

	return value;
}
 
Example #10
Source File: MappingAuditableBeanWrapperFactory.java    From spring-data-mybatis with Apache License 2.0 5 votes vote down vote up
private <P extends PersistentProperty<?>> TemporalAccessor setDateProperty(
		PersistentPropertyPaths<?, ? extends PersistentProperty<?>> property,
		TemporalAccessor value) {

	property.forEach(it -> this.accessor.setProperty(it, getDateValueToSet(value,
			it.getRequiredLeafProperty().getType(), accessor.getBean())));

	return value;
}
 
Example #11
Source File: ApplicationStructureBuilder.java    From moserp with Apache License 2.0 5 votes vote down vote up
private List<EntityProperty> buildProperties(Class<?> domainType) {
    List<EntityProperty> properties = new ArrayList<>();
    final PersistentEntity<?, ?> persistentEntity = persistentEntities.getPersistentEntity(domainType);
    JacksonMetadata jacksonMetadata = new JacksonMetadata(objectMapper, domainType);
    for (BeanPropertyDefinition definition : jacksonMetadata) {
        PersistentProperty<?> persistentProperty = persistentEntity.getPersistentProperty(definition.getInternalName());
        PropertyFactoryContext context = new PropertyFactoryContext(definition, jacksonMetadata, persistentProperty);
        PropertyFactory factory = getFactoryFor(context);
        if (factory != null) {
            properties.add(factory.create(context));
        }
    }
    return properties;
}
 
Example #12
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected PersistentProperty<?> getContentPropertyDefinition(
		PersistentEntity<?, ?> persistentEntity, String contentProperty) {
	PersistentProperty<?> prop = persistentEntity
			.getPersistentProperty(contentProperty);
	if (null == prop)
		throw new ResourceNotFoundException();

	return prop;
}
 
Example #13
Source File: BasicPropertyFactoryTest.java    From moserp with Apache License 2.0 5 votes vote down vote up
@Before
public void setupBasicMocks() {
    jacksonMetadata = new JacksonMetadata(new ObjectMapperBuilder().build(), SimpleClass.class);
    persistentProperty = mock(PersistentProperty.class);

    persistentEntity = mock(PersistentEntity.class);
    when(persistentEntity.getType()).thenReturn(SimpleClass.class);
    when(persistentProperty.getOwner()).thenReturn(persistentEntity);
}
 
Example #14
Source File: JsonSchemaBuilderTest.java    From moserp with Apache License 2.0 5 votes vote down vote up
public void setupMocks() throws URISyntaxException {
    configuration = mock(RepositoryRestConfiguration.class);
    when(configuration.getBaseUri()).thenReturn(new URI("http://localhost:8080/"));
    mappings = mock(ResourceMappings.class);
    metaData = mock(ResourceMetadata.class);
    when(metaData.getPath()).thenReturn(new Path("valueLists"));
    PersistentEntity persistentEntity = mock(PersistentEntity.class);
    when(persistentEntities.getPersistentEntity(any())).thenReturn(persistentEntity);
    PersistentProperty persistentProperty = mock(PersistentProperty.class);
    when(persistentEntity.getPersistentProperty(any(String.class))).thenReturn(persistentProperty);
    when(entityLinks.linkFor(any())).thenReturn(BaseUriLinkBuilder.create(new URI("http://localhost:8080/")));
    moduleRegistry = mock(ModuleRegistry.class);
    when(moduleRegistry.getBaseUriForResource(anyString())).thenReturn(new RestUri("http://localhost:8080/valueLists"));
}
 
Example #15
Source File: BasicPropertyFactoryTest.java    From moserp with Apache License 2.0 5 votes vote down vote up
@Before
public void setupBasicMocks() {
    jacksonMetadata = new JacksonMetadata(new ObjectMapperBuilder().build(), SimpleClass.class);
    persistentProperty = mock(PersistentProperty.class);

    persistentEntity = mock(PersistentEntity.class);
    when(persistentEntity.getType()).thenReturn(SimpleClass.class);
    when(persistentProperty.getOwner()).thenReturn(persistentEntity);
}
 
Example #16
Source File: JsonPropertyPreservingFieldNamingStrategy.java    From omh-dsu-ri with Apache License 2.0 5 votes vote down vote up
@Override
public String getFieldName(PersistentProperty<?> property) {

    JsonProperty jsonPropertyAnnotation = property.findAnnotation(JsonProperty.class);

    if (jsonPropertyAnnotation != null) {
        return jsonPropertyAnnotation.value();
    }
    else {
        return backingStrategy.getFieldName(property);
    }
}
 
Example #17
Source File: GeneratingIdAccessor.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link GeneratingIdAccessor} using the given {@link PersistentPropertyAccessor}, identifier property
 * and {@link IdentifierGenerator}.
 *
 * @param accessor must not be {@literal null}.
 * @param identifierProperty must not be {@literal null}.
 * @param generator must not be {@literal null}.
 */
GeneratingIdAccessor(PersistentPropertyAccessor<?> accessor, PersistentProperty<?> identifierProperty,
		IdentifierGenerator generator) {

	Assert.notNull(accessor, "PersistentPropertyAccessor must not be null!");
	Assert.notNull(identifierProperty, "Identifier property must not be null!");
	Assert.notNull(generator, "IdentifierGenerator must not be null!");

	this.accessor = accessor;
	this.identifierProperty = identifierProperty;
	this.generator = generator;
}
 
Example #18
Source File: SpannerRepositoryIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void existsTest() {
	Trade trade = Trade.aTrade();
	this.tradeRepository.save(trade);
	SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(Trade.class);
	PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(trade);
	PersistentProperty idProperty = persistentEntity.getIdProperty();
	Key key = (Key) accessor.getProperty(idProperty);
	assertThat(this.tradeRepository.existsById(key)).isTrue();
	this.tradeRepository.delete(trade);
	assertThat(this.tradeRepository.existsById(key)).isFalse();
}
 
Example #19
Source File: CypherQueryCreator.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
private boolean isLastNode(PersistentProperty<?> persistentProperty) {

			// size - 1 = last index
			// size - 2 = property on last node
			// size - 3 = last node itself
			return propertyPathList.indexOf(persistentProperty) > propertyPathList.size() - 3;
		}
 
Example #20
Source File: XiaoEUKResultMapper.java    From youkefu with Apache License 2.0 5 votes vote down vote up
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {

		if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {

			ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
			PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
			
			// Only deal with String because ES generated Ids are strings !
			if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
				persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
			}
		}
	}
 
Example #21
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 5 votes vote down vote up
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {

		if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {

			ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
			PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
			
			// Only deal with String because ES generated Ids are strings !
			if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
				persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
			}
		}
	}
 
Example #22
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
	DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext
			.getPersistentEntity(entity.getClass());
	List<Entity> entitiesToSave = new ArrayList<>();
	datastorePersistentEntity.doWithAssociations((AssociationHandler) (association) -> {
		PersistentProperty persistentProperty = association.getInverse();
		PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
		Object val = accessor.getProperty(persistentProperty);
		if (val == null) {
			return;
		}
		Value<?> value;
		if (LazyUtil.isLazyAndNotLoaded(val)) {
			value = LazyUtil.getKeys(val);
		}
		else if (persistentProperty.isCollectionLike()) {
			Iterable<?> iterableVal = (Iterable<?>) ValueUtil.toListIfArray(val);
			entitiesToSave.addAll(getEntitiesForSave(iterableVal, persistedEntities));
			List<KeyValue> keyValues = StreamSupport.stream((iterableVal).spliterator(), false)
					.map((o) -> KeyValue.of(this.getKey(o, false)))
					.collect(Collectors.toList());
			value = ListValue.of(keyValues);

		}
		else {
			entitiesToSave.addAll(getEntitiesForSave(Collections.singletonList(val), persistedEntities));
			Key key = getKey(val, false);
			value = KeyValue.of(key);
		}
		builder.set(((DatastorePersistentProperty) persistentProperty).getFieldName(), value);
	});
	return entitiesToSave;
}
 
Example #23
Source File: DatastoreServiceObjectToKeyFactory.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public Key getKeyFromObject(Object entity, DatastorePersistentEntity datastorePersistentEntity) {
	Assert.notNull(entity, "Cannot get key for null entity object.");
	Assert.notNull(datastorePersistentEntity, "Persistent entity must not be null.");
	PersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
	Object idVal = datastorePersistentEntity.getPropertyAccessor(entity).getProperty(idProp);
	if (idVal == null) {
		return null;
	}
	else {
		return getKeyFromId(idVal, datastorePersistentEntity.kindName());
	}
}
 
Example #24
Source File: DatastoreServiceObjectToKeyFactory.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public Key allocateKeyForObject(Object entity,
		DatastorePersistentEntity datastorePersistentEntity, Key... ancestors) {
	Assert.notNull(entity, "Cannot get key for null entity object.");
	Assert.notNull(datastorePersistentEntity, "Persistent entity must not be null.");
	PersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();

	Class idPropType = idProp.getType();

	if (!idPropType.equals(Key.class) && !idPropType.equals(Long.class)) {
		throw new DatastoreDataException("Cloud Datastore can only allocate IDs for Long and Key properties. " +
				"Cannot allocate for type: " + idPropType);
	}

	KeyFactory keyFactory = getKeyFactory().setKind(datastorePersistentEntity.kindName());
	if (ancestors != null && ancestors.length > 0) {
		if (!idPropType.equals(Key.class)) {
			throw new DatastoreDataException("Only Key types are allowed for descendants id");
		}
		for (Key ancestor : ancestors) {
			keyFactory.addAncestor(DatastoreTemplate.keyToPathElement(ancestor));
		}
	}
	Key allocatedKey = this.datastore.get().allocateId(keyFactory.newKey());

	Object value;
	if (idPropType.equals(Key.class)) {
		value = allocatedKey;
	}
	else if (idPropType.equals(Long.class)) {
		value = allocatedKey.getId();
	}
	else {
		value = allocatedKey.getId().toString();
	}

	datastorePersistentEntity.getPropertyAccessor(entity).setProperty(idProp, value);
	return allocatedKey;
}
 
Example #25
Source File: SpannerMutationFactoryImpl.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Mutation delete(Class<T> entityClass, Iterable<? extends T> entities) {
	SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext
			.getPersistentEntity(entityClass);
	KeySet.Builder builder = KeySet.newBuilder();
	for (T entity : entities) {
		PersistentPropertyAccessor accessor = persistentEntity
				.getPropertyAccessor(entity);
		PersistentProperty idProperty = persistentEntity.getIdProperty();
		Key value = (Key) accessor.getProperty(idProperty);

		builder.addKey(value);
	}
	return delete(entityClass, builder.build());
}
 
Example #26
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected Object getContentProperty(Object domainObj, PersistentProperty<?> property,
		String contentId) {

	PersistentPropertyAccessor accessor = property.getOwner()
			.getPropertyAccessor(domainObj);
	Object contentPropertyObject = accessor.getProperty(property);

	// multi-valued property?
	if (PersistentEntityUtils.isPropertyMultiValued(property)) {
		if (property.isArray()) {
			throw new UnsupportedOperationException();
		}
		else if (property.isCollectionLike()) {
			contentPropertyObject = findContentPropertyObjectInSet(contentId,
					(Collection<?>) contentPropertyObject);
		}
	}

	if (contentPropertyObject == null) {
		throw new ResourceNotFoundException();
	}

	if (BeanUtils.hasFieldWithAnnotation(contentPropertyObject, ContentId.class)) {
		if (BeanUtils.getFieldWithAnnotation(contentPropertyObject,
				ContentId.class) == null) {
			throw new ResourceNotFoundException();
		}
	}

	return contentPropertyObject;
}
 
Example #27
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetIdProperty() {
	SpannerPersistentEntity entity = new SpannerMappingContext()
			.getPersistentEntity(MultiIdsEntity.class);

	PersistentProperty idProperty = entity.getIdProperty();

	MultiIdsEntity t = new MultiIdsEntity();
	entity.getPropertyAccessor(t).setProperty(idProperty, Key.of("blah", 123L, 123.45D));

	assertThat(t.id).isEqualTo("blah");
	assertThat(t.id2).isEqualTo(123L);
	assertThat(t.id3).isEqualTo(123.45D);
}
 
Example #28
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetIdPropertyLongerKey() {
	this.thrown.expect(SpannerDataException.class);
	this.thrown.expectMessage(
			"The number of key parts is not equal to the number of primary key properties");

	SpannerPersistentEntity entity = new SpannerMappingContext()
			.getPersistentEntity(MultiIdsEntity.class);

	PersistentProperty idProperty = entity.getIdProperty();

	MultiIdsEntity t = new MultiIdsEntity();
	entity.getPropertyAccessor(t).setProperty(idProperty, Key.of("blah", 123L, 123.45D, "abc"));
}
 
Example #29
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetIdPropertyNullKey() {
	this.thrown.expect(SpannerDataException.class);
	this.thrown.expectMessage(
			"The number of key parts is not equal to the number of primary key properties");

	SpannerPersistentEntity entity = new SpannerMappingContext()
			.getPersistentEntity(MultiIdsEntity.class);

	PersistentProperty idProperty = entity.getIdProperty();

	MultiIdsEntity t = new MultiIdsEntity();
	entity.getPropertyAccessor(t).setProperty(idProperty, null);
}
 
Example #30
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected void setContentProperty(Object domainObj, PersistentProperty<?> property,
		String contentId, Object newValue) {

	PersistentPropertyAccessor accessor = property.getOwner()
			.getPropertyAccessor(domainObj);
	Object contentPropertyObject = accessor.getProperty(property);
	if (contentPropertyObject == null)
		return;
	else if (!PersistentEntityUtils.isPropertyMultiValued(property)) {
		accessor.setProperty(property, newValue);
	}
	else {
		// handle multi-valued
		if (property.isArray()) {
			throw new UnsupportedOperationException();
		}
		else if (property.isCollectionLike()
				&& contentPropertyObject instanceof Set) {
			@SuppressWarnings("unchecked")
			Set<Object> contentSet = (Set<Object>) contentPropertyObject;
			Object oldValue = findContentPropertyObjectInSet(contentId, contentSet);
			contentSet.remove(oldValue);
			if (newValue != null)
				contentSet.add(newValue);
		}
	}
}