DATAKV-184 - Adapt to API changes in mapping subsystem.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.core;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.mapping.IdentifierAccessor;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
@@ -26,8 +23,9 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* {@link IdentifierAccessor} adding a {@link #getOrGenerateIdentifier()} to automatically generate an identifier and
|
||||
* set it on the underling bean instance.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @see #getOrGenerateIdentifier()
|
||||
*/
|
||||
class GeneratingIdAccessor implements IdentifierAccessor {
|
||||
@@ -39,7 +37,7 @@ class GeneratingIdAccessor implements IdentifierAccessor {
|
||||
/**
|
||||
* 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}.
|
||||
@@ -61,26 +59,26 @@ class GeneratingIdAccessor implements IdentifierAccessor {
|
||||
* @see org.springframework.data.keyvalue.core.IdentifierAccessor#getIdentifier()
|
||||
*/
|
||||
@Override
|
||||
public Optional<Object> getIdentifier() {
|
||||
public Object getIdentifier() {
|
||||
return accessor.getProperty(identifierProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the identifier value of the backing bean or generates a new one using the configured
|
||||
* {@link IdentifierGenerator}.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Object getOrGenerateIdentifier() {
|
||||
|
||||
Optional<Object> existingIdentifier = getIdentifier();
|
||||
Object existingIdentifier = getIdentifier();
|
||||
|
||||
if (existingIdentifier.isPresent()) {
|
||||
return existingIdentifier.get();
|
||||
if (existingIdentifier != null) {
|
||||
return existingIdentifier;
|
||||
}
|
||||
|
||||
Object generatedIdentifier = generator.generateIdentifierOfType(identifierProperty.getTypeInformation());
|
||||
accessor.setProperty(identifierProperty, Optional.ofNullable(generatedIdentifier));
|
||||
accessor.setProperty(identifierProperty, generatedIdentifier);
|
||||
|
||||
return generatedIdentifier;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPublisherAware {
|
||||
|
||||
@@ -134,8 +135,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
KeyValuePersistentEntity<?, ?> entity = getKeyValuePersistentEntity(objectToInsert);
|
||||
|
||||
GeneratingIdAccessor generatingIdAccessor = new GeneratingIdAccessor(entity.getPropertyAccessor(objectToInsert),
|
||||
entity.getIdProperty()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Unable to extract 'id' for object to be deleted")),
|
||||
entity.getIdProperty(),
|
||||
identifierGenerator);
|
||||
Object id = generatingIdAccessor.getOrGenerateIdentifier();
|
||||
|
||||
@@ -144,10 +144,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
}
|
||||
|
||||
private KeyValuePersistentEntity<?, ?> getKeyValuePersistentEntity(Object objectToInsert) {
|
||||
|
||||
return this.mappingContext.getPersistentEntity(ClassUtils.getUserClass(objectToInsert))
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
String.format("Unable to find PersistentEntity for %s", ObjectUtils.nullSafeClassName(objectToInsert))));
|
||||
return this.mappingContext.getRequiredPersistentEntity(ClassUtils.getUserClass(objectToInsert));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -197,7 +194,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
String.format("Cannot determine id for type %s", ClassUtils.getUserClass(objectToUpdate)));
|
||||
}
|
||||
|
||||
update((Serializable) entity.getIdentifierAccessor(objectToUpdate).getIdentifier().get(), objectToUpdate);
|
||||
update((Serializable) entity.getIdentifierAccessor(objectToUpdate).getRequiredIdentifier(), objectToUpdate);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -331,8 +328,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
Class<T> type = (Class<T>) ClassUtils.getUserClass(objectToDelete);
|
||||
KeyValuePersistentEntity<?, ?> entity = getKeyValuePersistentEntity(objectToDelete);
|
||||
|
||||
return delete((Serializable) entity.getIdentifierAccessor(objectToDelete).getIdentifier()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Unable to extract 'id' for object to be deleted")), type);
|
||||
return delete((Serializable) entity.getIdentifierAccessor(objectToDelete).getIdentifier(), type);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -485,7 +481,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
}
|
||||
|
||||
private String resolveKeySpace(Class<?> type) {
|
||||
return this.mappingContext.getPersistentEntity(type).get().getKeySpace();
|
||||
return this.mappingContext.getRequiredPersistentEntity(type).getKeySpace();
|
||||
}
|
||||
|
||||
private RuntimeException resolveExceptionIfPossible(RuntimeException e) {
|
||||
|
||||
@@ -51,6 +51,7 @@ import org.springframework.util.ClassUtils;
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class KeyValueRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
@@ -113,10 +114,9 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
|
||||
PersistentEntity<T, ?> entity = (PersistentEntity<T, ?>) context.getPersistentEntity(domainClass).get();
|
||||
PersistentEntityInformation<T, ID> entityInformation = new PersistentEntityInformation<>(entity);
|
||||
PersistentEntity<T, ?> entity = (PersistentEntity<T, ?>) context.getRequiredPersistentEntity(domainClass);
|
||||
|
||||
return entityInformation;
|
||||
return new PersistentEntityInformation<>(entity);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @param <T>
|
||||
* @param <ID>
|
||||
*/
|
||||
@@ -97,7 +98,7 @@ public class SimpleKeyValueRepository<T, ID extends Serializable> implements Key
|
||||
if (entityInformation.isNew(entity)) {
|
||||
operations.insert(entity);
|
||||
} else {
|
||||
operations.update(entityInformation.getId(entity).get(), entity);
|
||||
operations.update(entityInformation.getRequiredId(entity), entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
@@ -188,8 +189,7 @@ public class SimpleKeyValueRepository<T, ID extends Serializable> implements Key
|
||||
*/
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
deleteById(entityInformation.getId(entity)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Cannot delete entity with 'null' id.")));
|
||||
deleteById(entityInformation.getRequiredId(entity));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user