DATACMNS-1322 - Added @Immutable to allow users to express a persistent entity to be considered immutable.

This is needed for downstream projects that attempt to merge persistent entity instances and previously didn't have a chance to detect that an object had to be set as-is instead of being merged recursively.
This commit is contained in:
Oliver Gierke
2018-07-12 10:26:16 +02:00
parent 1f4d494f83
commit 3c2522df9a
4 changed files with 66 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.mapping;
import java.lang.annotation.Annotation;
import java.util.Iterator;
import org.springframework.data.annotation.Immutable;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
@@ -305,4 +306,14 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> extends It
* @return whether the given bean is considered a new instance.
*/
boolean isNew(Object bean);
/**
* Returns whether the entity is considered immutable, i.e. clients shouldn't attempt to change instances via the
* {@link PersistentPropertyAccessor} obtained via {@link #getPropertyAccessor(Object)}.
*
* @return
* @see Immutable
* @since 2.1
*/
boolean isImmutable();
}

View File

@@ -33,6 +33,7 @@ import java.util.TreeSet;
import java.util.stream.Collectors;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.annotation.Immutable;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.Alias;
@@ -93,6 +94,7 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
private final Lazy<Alias> typeAlias;
private final Lazy<IsNewStrategy> isNewStrategy;
private final Lazy<Boolean> isImmutable;
/**
* Creates a new {@link BasicPersistentEntity} from the given {@link TypeInformation}.
@@ -130,7 +132,7 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
this.isNewStrategy = Lazy.of(() -> Persistable.class.isAssignableFrom(information.getType()) //
? PersistableIsNewStrategy.INSTANCE
: PersistentEntityIsNewStrategy.of(this));
this.isImmutable = Lazy.of(() -> isAnnotationPresent(Immutable.class));
}
/*
@@ -496,6 +498,15 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
return isNewStrategy.get().isNew(bean);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.PersistentEntity#isImmutable()
*/
@Override
public boolean isImmutable() {
return isImmutable.get();
}
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()