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:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user