use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class MongoTemplate method populateIdIfNecessary.
/**
* Populates the id property of the saved object, if it's not set already.
*
* @param savedObject
* @param id
*/
protected void populateIdIfNecessary(Object savedObject, Object id) {
if (id == null) {
return;
}
if (savedObject instanceof Document) {
Document document = (Document) savedObject;
document.put(ID_FIELD, id);
return;
}
MongoPersistentProperty idProperty = getIdPropertyFor(savedObject.getClass());
if (idProperty != null) {
ConversionService conversionService = mongoConverter.getConversionService();
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
Object value = accessor.getProperty(idProperty);
if (value == null) {
new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProperty, id);
}
}
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method populateIdIfNecessary.
/**
* Populates the id property of the saved object, if it's not set already.
*
* @param savedObject
* @param id
*/
private void populateIdIfNecessary(Object savedObject, @Nullable Object id) {
if (id == null) {
return;
}
if (savedObject instanceof Document) {
Document Document = (Document) savedObject;
Document.put(ID_FIELD, id);
return;
}
MongoPersistentProperty idProp = getIdPropertyFor(savedObject.getClass());
if (idProp == null) {
return;
}
ConversionService conversionService = mongoConverter.getConversionService();
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
if (accessor.getProperty(idProp) != null) {
return;
}
new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id);
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-mongodb by spring-projects.
the class DtoInstantiatingConverter method convert.
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Object convert(Object source) {
if (targetType.isInterface()) {
return source;
}
final PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
final PersistentPropertyAccessor sourceAccessor = sourceEntity.getPropertyAccessor(source);
final PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType);
final PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity.getPersistenceConstructor();
@SuppressWarnings({ "rawtypes", "unchecked" }) Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {
@Override
public Object getParameterValue(Parameter parameter) {
return sourceAccessor.getProperty(sourceEntity.getPersistentProperty(parameter.getName().toString()));
}
});
final PersistentPropertyAccessor dtoAccessor = targetEntity.getPropertyAccessor(dto);
targetEntity.doWithProperties(new SimplePropertyHandler() {
@Override
public void doWithPersistentProperty(PersistentProperty<?> property) {
if (constructor.isConstructorParameter(property)) {
return;
}
dtoAccessor.setProperty(property, sourceAccessor.getProperty(sourceEntity.getPersistentProperty(property.getName())));
}
});
return dto;
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-commons by spring-projects.
the class BasicPersistentEntityUnitTests method returnsGeneratedPropertyAccessorForPropertyAccessor.
// DATACMNS-809
@Test
public void returnsGeneratedPropertyAccessorForPropertyAccessor() {
SampleMappingContext context = new SampleMappingContext();
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
Entity value = new Entity();
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
assertThat(accessor).isNotInstanceOf(BeanWrapper.class);
assertThat(accessor.getClass().getName()).contains("_Accessor_");
assertThat(accessor.getBean()).isEqualTo(value);
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-data-commons by spring-projects.
the class ClassGeneratingPropertyAccessorFactoryDatatypeTests method shouldSetAndGetProperty.
// DATACMNS-809
@Test
public void shouldSetAndGetProperty() throws Exception {
assertThat(getProperty(bean, propertyName)).satisfies(property -> {
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
persistentPropertyAccessor.setProperty(property, value);
assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo(value);
});
}
Aggregations