Java Code Examples for org.springframework.data.mapping.PersistentPropertyAccessor#getBean()
The following examples show how to use
org.springframework.data.mapping.PersistentPropertyAccessor#getBean() .
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: IdPopulator.java From sdn-rx with Apache License 2.0 | 5 votes |
Object populateIfNecessary(Object entity) { Assert.notNull(entity, "Entity may not be null!"); Neo4jPersistentEntity<?> nodeDescription = neo4jMappingContext.getRequiredPersistentEntity(entity.getClass()); IdDescription idDescription = nodeDescription.getIdDescription(); // Filter in two steps to avoid unnecessary object creation. if (!idDescription.isExternallyGeneratedId()) { return entity; } PersistentPropertyAccessor propertyAccessor = nodeDescription.getPropertyAccessor(entity); Neo4jPersistentProperty idProperty = nodeDescription.getRequiredIdProperty(); // Check existing ID if (propertyAccessor.getProperty(idProperty) != null) { return entity; } IdGenerator<?> idGenerator; // Get or create the shared generator // Ref has precedence over class Optional<String> optionalIdGeneratorRef = idDescription.getIdGeneratorRef(); if (optionalIdGeneratorRef.isPresent()) { idGenerator = neo4jMappingContext .getIdGenerator(optionalIdGeneratorRef.get()).orElseThrow(() -> new IllegalStateException( "Id generator named " + optionalIdGeneratorRef.get() + " not found!")); } else { // At this point, the class must be present, so we don't check the optional not anymore idGenerator = neo4jMappingContext.getOrCreateIdGeneratorOfType(idDescription.getIdGeneratorClass().get()); } propertyAccessor.setProperty(idProperty, idGenerator.generateId(nodeDescription.getPrimaryLabel(), entity)); return propertyAccessor.getBean(); }
Example 2
Source File: Neo4jTemplate.java From sdn-rx with Apache License 2.0 | 5 votes |
private <T> T saveImpl(T instance, @Nullable String inDatabase) { Neo4jPersistentEntity entityMetaData = neo4jMappingContext.getPersistentEntity(instance.getClass()); T entityToBeSaved = eventSupport.maybeCallBeforeBind(instance); DynamicLabels dynamicLabels = determineDynamicLabels(entityToBeSaved, entityMetaData, inDatabase); Optional<Long> optionalInternalId = neo4jClient .query(() -> renderer.render(cypherGenerator.prepareSaveOf(entityMetaData, dynamicLabels))) .in(inDatabase) .bind((T) entityToBeSaved) .with(neo4jMappingContext.getRequiredBinderFunctionFor((Class<T>) entityToBeSaved.getClass())) .fetchAs(Long.class).one(); if (entityMetaData.hasVersionProperty() && !optionalInternalId.isPresent()) { throw new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE); } PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(entityToBeSaved); if (!entityMetaData.isUsingInternalIds()) { processRelations(entityMetaData, entityToBeSaved, inDatabase); return entityToBeSaved; } else { propertyAccessor.setProperty(entityMetaData.getRequiredIdProperty(), optionalInternalId.get()); processRelations(entityMetaData, entityToBeSaved, inDatabase); return propertyAccessor.getBean(); } }
Example 3
Source File: TypicalEntityReaderBenchmark.java From spring-data-dev-tools with Apache License 2.0 | 4 votes |
/** * Typical code used to read entities in {@link org.springframework.data.convert.EntityReader}. * * @param data * @param classToRead * @param queryCustomConversions {@literal true} to call {@link CustomConversions#hasCustomReadTarget(Class, Class)}. * @return */ @SuppressWarnings("unchecked") private Object read(Map<String, Object> data, Class<?> classToRead, boolean queryCustomConversions) { if (queryCustomConversions) { customConversions.hasCustomReadTarget(Map.class, classToRead); } MyPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(classToRead); PreferredConstructor<?, MyPersistentProperty> constructor = persistentEntity.getPersistenceConstructor(); ParameterValueProvider<MyPersistentProperty> provider = constructor.isNoArgConstructor() // ? NONE // : new ParameterValueProvider<MyPersistentProperty>() { @Override public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) { return (T) getValue(data, parameter.getName(), parameter.getType().getType(), queryCustomConversions); } }; EntityInstantiator instantiator = instantiators.getInstantiatorFor(persistentEntity); Object instance = instantiator.createInstance(persistentEntity, provider); if (!persistentEntity.requiresPropertyPopulation()) { return instance; } PropertyValueProvider<MyPersistentProperty> valueProvider = new PropertyValueProvider<MyPersistentProperty>() { @Override public <T> T getPropertyValue(MyPersistentProperty property) { return (T) getValue(data, property.getName(), property.getType(), queryCustomConversions); } }; PersistentPropertyAccessor<?> accessor = new ConvertingPropertyAccessor<>( persistentEntity.getPropertyAccessor(instance), conversionService); readProperties(data, persistentEntity, valueProvider, accessor); return accessor.getBean(); }
Example 4
Source File: MappingCrateConverter.java From spring-data-crate with Apache License 2.0 | 4 votes |
/** * 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; }