diff --git a/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java index 7ec09d3..971346b 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java +++ b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java @@ -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 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 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; } diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java index ae9569c..6238fb6 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java @@ -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 type = (Class) 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) { diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java index 0c91be7..1ea8b6f 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java @@ -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 EntityInformation getEntityInformation(Class domainClass) { - PersistentEntity entity = (PersistentEntity) context.getPersistentEntity(domainClass).get(); - PersistentEntityInformation entityInformation = new PersistentEntityInformation<>(entity); + PersistentEntity entity = (PersistentEntity) context.getRequiredPersistentEntity(domainClass); - return entityInformation; + return new PersistentEntityInformation<>(entity); } /* diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java index 5b8c66c..502c906 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java @@ -33,6 +33,7 @@ import org.springframework.util.Assert; /** * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch * @param * @param */ @@ -97,7 +98,7 @@ public class SimpleKeyValueRepository 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 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)); } /*