DATAJPA-1143 - Adapt to API changes in mapping subsystem.

Rolled back API usage of Optional in mapping APIs.
This commit is contained in:
Oliver Gierke
2017-07-03 14:49:27 +02:00
committed by Mark Paluch
parent 7cfa1be6fd
commit f1f5ca98c1
10 changed files with 78 additions and 67 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.data.jpa.mapping;
import java.util.Comparator;
import java.util.Optional;
import org.springframework.data.annotation.Version;
import org.springframework.data.jpa.provider.ProxyIdAccessor;
@@ -51,7 +50,7 @@ class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentP
*/
public JpaPersistentEntityImpl(TypeInformation<T> information, ProxyIdAccessor proxyIdAccessor) {
super(information, Optional.empty());
super(information, null);
Assert.notNull(proxyIdAccessor, "ProxyIdAccessor must not be null!");
this.proxyIdAccessor = proxyIdAccessor;
@@ -84,13 +83,11 @@ class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentP
super.verify();
Optional<JpaPersistentProperty> versionProperty = getVersionProperty();
getVersionProperty();
if (!versionProperty.isPresent()) {
return;
}
JpaPersistentProperty versionProperty = getVersionProperty();
if (versionProperty.get().isAnnotationPresent(Version.class)) {
if (versionProperty != null && versionProperty.isAnnotationPresent(Version.class)) {
throw new IllegalArgumentException(String.format(INVALID_VERSION_ANNOTATION, versionProperty));
}
}
@@ -131,10 +128,10 @@ class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentP
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
*/
@Override
public Optional<Object> getIdentifier() {
public Object getIdentifier() {
return proxyIdAccessor.shouldUseAccessorFor(bean) //
? Optional.ofNullable(proxyIdAccessor.getIdentifierFrom(bean))//
? proxyIdAccessor.getIdentifierFrom(bean)//
: super.getIdentifier();
}
}

View File

@@ -19,7 +19,6 @@ import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.persistence.Access;
@@ -47,7 +46,6 @@ import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
@@ -90,8 +88,8 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
UPDATEABLE_ANNOTATIONS = Collections.unmodifiableSet(annotations);
}
private final Optional<Boolean> usePropertyAccess;
private final Optional<TypeInformation<?>> associationTargetType;
private final Boolean usePropertyAccess;
private final TypeInformation<?> associationTargetType;
private final boolean updateable;
private final JpaMetamodel metamodel;
@@ -122,7 +120,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*/
@Override
public Class<?> getActualType() {
return associationTargetType.isPresent() ? associationTargetType.get().getType() : super.getActualType();
return associationTargetType != null ? associationTargetType.getType() : super.getActualType();
}
/*
@@ -131,7 +129,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*/
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
return associationTargetType.isPresent() ? Collections.singleton(associationTargetType.get())
return associationTargetType != null ? Collections.singleton(associationTargetType)
: super.getPersistentEntityType();
}
@@ -160,8 +158,10 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
@Override
public boolean isAssociation() {
if (ASSOCIATION_ANNOTATIONS.stream().anyMatch(it -> findAnnotation(it).isPresent())) {
return true;
for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) {
if (findAnnotation(annotationType) != null) {
return true;
}
}
return getType().isAnnotationPresent(Embeddable.class);
@@ -191,7 +191,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*/
@Override
public boolean usePropertyAccess() {
return usePropertyAccess.orElseGet(() -> super.usePropertyAccess());
return usePropertyAccess != null ? usePropertyAccess : super.usePropertyAccess();
}
/*
@@ -220,29 +220,34 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*
* @return
*/
private Optional<Boolean> detectPropertyAccess() {
private Boolean detectPropertyAccess() {
Optional<org.springframework.data.annotation.AccessType> accessType = findAnnotation(
org.springframework.data.annotation.AccessType accessType = findAnnotation(
org.springframework.data.annotation.AccessType.class);
if (accessType.isPresent()) {
return accessType.map(it -> Type.PROPERTY.equals(it.value()));
if (accessType != null) {
return Type.PROPERTY.equals(accessType.value());
}
Optional<Access> access = findAnnotation(Access.class);
Access access = findAnnotation(Access.class);
if (access.isPresent()) {
return access.map(it -> AccessType.PROPERTY.equals(it.value()));
if (access != null) {
return AccessType.PROPERTY.equals(access.value());
}
accessType = findPropertyOrOwnerAnnotation(org.springframework.data.annotation.AccessType.class);
if (accessType.isPresent()) {
return accessType.map(it -> Type.PROPERTY.equals(it.value()));
if (accessType != null) {
return Type.PROPERTY.equals(accessType.value());
}
access = findPropertyOrOwnerAnnotation(Access.class);
return access.map(t -> AccessType.PROPERTY.equals(t.value()));
if (access != null) {
return AccessType.PROPERTY.equals(access.value());
}
return null;
}
/**
@@ -250,18 +255,30 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*
* @return
*/
private Optional<TypeInformation<?>> detectAssociationTargetType() {
private TypeInformation<?> detectAssociationTargetType() {
if (!isAssociation()) {
return Optional.empty();
return null;
}
return ASSOCIATION_ANNOTATIONS.stream()//
.flatMap(it -> Optionals.toStream(findAnnotation(it)))//
.map(it -> AnnotationUtils.getValue(it, "targetEntity"))//
.filter(it -> it != null && !void.class.equals(it))//
.map(it -> (Class<?>) it)//
.findFirst().map(it -> (TypeInformation<?>) ClassTypeInformation.from(it));
for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) {
Annotation annotation = findAnnotation(annotationType);
if (annotation == null) {
continue;
}
Object entityValue = AnnotationUtils.getValue(annotation, "targetEntity");
if (entityValue == null || entityValue.equals(void.class)) {
continue;
}
return ClassTypeInformation.from((Class<?>) entityValue);
}
return null;
}
/**
@@ -272,9 +289,17 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
*/
private final boolean detectUpdatability() {
return !UPDATEABLE_ANNOTATIONS.stream()//
.flatMap(it -> Optionals.toStream(findAnnotation(it)))//
.map(it -> AnnotationUtils.getValue(it, "updatable"))//
.anyMatch(it -> it.equals(Boolean.FALSE));
for (Class<? extends Annotation> annotationType : UPDATEABLE_ANNOTATIONS) {
Annotation annotation = findAnnotation(annotationType);
if (annotation == null) {
continue;
}
return (boolean) AnnotationUtils.getValue(annotation, "updatable");
}
return true;
}
}

View File

@@ -19,7 +19,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.persistence.IdClass;
@@ -141,12 +140,12 @@ public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSu
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
*/
@SuppressWarnings("unchecked")
public Optional<ID> getId(T entity) {
public ID getId(T entity) {
BeanWrapper entityWrapper = new DirectFieldAccessFallbackBeanWrapper(entity);
if (idMetadata.hasSimpleId()) {
return Optional.ofNullable((ID) entityWrapper.getPropertyValue(idMetadata.getSimpleIdAttribute().getName()));
return (ID) entityWrapper.getPropertyValue(idMetadata.getSimpleIdAttribute().getName());
}
BeanWrapper idWrapper = new IdentifierDerivingDirectFieldAccessFallbackBeanWrapper(idMetadata.getType(), metamodel);
@@ -162,7 +161,7 @@ public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSu
idWrapper.setPropertyValue(attribute.getName(), propertyValue);
}
return partialIdValueFound ? Optional.ofNullable((ID) idWrapper.getWrappedInstance()) : Optional.empty();
return partialIdValueFound ? (ID) idWrapper.getWrappedInstance() : null;
}
/*

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.data.jpa.repository.support;
import java.util.Optional;
import javax.persistence.metamodel.Metamodel;
import org.springframework.data.domain.Persistable;
@@ -54,7 +52,7 @@ public class JpaPersistableEntityInformation<T extends Persistable<ID>, ID>
* @see org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation#getId(java.lang.Object)
*/
@Override
public Optional<ID> getId(T entity) {
return Optional.ofNullable(entity.getId());
public ID getId(T entity) {
return entity.getId();
}
}

View File

@@ -19,7 +19,6 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.Optional;
import javax.persistence.EntityManager;
@@ -139,7 +138,7 @@ public class JpaMetamodelMappingContextIntegrationTests {
JpaPersistentEntity<?> entity = context.getRequiredPersistentEntity(Product.class);
IdentifierAccessor accessor = entity.getIdentifierAccessor(loadedProduct);
assertThat(accessor.getIdentifier(), is(Optional.of(category.getProduct().getId())));
assertThat(accessor.getIdentifier(), is(category.getProduct().getId()));
assertThat(loadedProduct, is(instanceOf(HibernateProxy.class)));
assertThat(((HibernateProxy) loadedProduct).getHibernateLazyInitializer().isUninitialized(), is(true));

View File

@@ -21,7 +21,6 @@ import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.Optional;
import javax.persistence.Access;
import javax.persistence.AccessType;
@@ -74,7 +73,7 @@ public class JpaPersistentPropertyImplUnitTests {
@Test // DATAJPA-376
public void considersJpaTransientFieldsAsTransient() {
assertThat(entity.getPersistentProperty("transientProp"), is(Optional.empty()));
assertThat(entity.getPersistentProperty("transientProp"), is(nullValue()));
}
@Test // DATAJPA-484

View File

@@ -20,7 +20,6 @@ import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.Optional;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
@@ -79,8 +78,8 @@ public class JpaEntityInformationSupportUnitTests {
return null;
}
public Optional<ID> getId(T entity) {
return Optional.empty();
public ID getId(T entity) {
return null;
}
public Class<ID> getIdType() {

View File

@@ -22,7 +22,6 @@ import static org.springframework.data.jpa.repository.support.JpaEntityInformati
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Optional;
import javax.persistence.Access;
import javax.persistence.AccessType;
@@ -40,7 +39,6 @@ import org.hibernate.Version;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.domain.sample.ConcreteType1;
import org.springframework.data.jpa.domain.sample.Item;
@@ -112,7 +110,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
em);
Object id = information.getId(entity);
assertThat(id, is(Optional.of(new PersistableWithIdClassPK(2L, 4L))));
assertThat(id, is(new PersistableWithIdClassPK(2L, 4L)));
}
@Test // DATAJPA-413
@@ -123,7 +121,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
JpaEntityInformation<Item, ?> information = getEntityInformation(Item.class, em);
Object id = information.getId(item);
assertThat(id, is(Optional.of(new ItemId(2, 1))));
assertThat(id, is(new ItemId(2, 1)));
}
@Test // DATAJPA-413
@@ -137,7 +135,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
JpaEntityInformation<ItemSite, ?> information = getEntityInformation(ItemSite.class, em);
Object id = information.getId(itemSite);
assertThat(id, is(Optional.of(new ItemSiteId(new ItemId(1, 2), 3))));
assertThat(id, is(new ItemSiteId(new ItemId(1, 2), 3)));
}
@Test // DATAJPA-413
@@ -151,7 +149,7 @@ public class JpaMetamodelEntityInformationIntegrationTests {
JpaEntityInformation<ItemSite, ?> information = getEntityInformation(ItemSite.class, em);
Object id = information.getId(itemSite);
assertThat(id, is(Optional.of(new ItemSiteId(new ItemId(1, null), 3))));
assertThat(id, is(new ItemSiteId(new ItemId(1, null), 3)));
}
@Test // DATAJPA-119

View File

@@ -22,7 +22,6 @@ import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.persistence.metamodel.IdentifiableType;
@@ -78,7 +77,7 @@ public class JpaMetamodelEntityInformationUnitTests {
PersistableWithIdClass.class, metamodel);
PersistableWithIdClass entity = new PersistableWithIdClass(null, null);
assertThat(information.getId(entity), is(Optional.empty()));
assertThat(information.getId(entity), is(nullValue()));
entity = new PersistableWithIdClass(2L, null);
assertThat(information.getId(entity), is(notNullValue()));

View File

@@ -19,8 +19,6 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.Type;
@@ -64,11 +62,11 @@ public class JpaPersistableEntityInformationUnitTests {
Foo foo = new Foo();
assertThat(entityInformation.isNew(foo), is(false));
assertThat(entityInformation.getId(foo), is(Optional.empty()));
assertThat(entityInformation.getId(foo), is(nullValue()));
foo.id = 1L;
assertThat(entityInformation.isNew(foo), is(true));
assertThat(entityInformation.getId(foo), is(Optional.of(1L)));
assertThat(entityInformation.getId(foo), is(1L));
}
@SuppressWarnings("serial")