/** * Creates a {@link MappingContextTypeInformationMapper} from the given {@link MappingContext}. Inspects all * {@link PersistentEntity} instances for alias information and builds a {@link Map} of aliases to types from it. * * @param mappingContext must not be {@literal null}. */ public MappingContextTypeInformationMapper(MappingContext<? extends PersistentEntity<?, ?>, ?> mappingContext) { Assert.notNull(mappingContext, "MappingContext must not be null!"); this.typeMap = new ConcurrentHashMap<>(); this.mappingContext = mappingContext; for (PersistentEntity<?, ?> entity : mappingContext.getPersistentEntities()) { verify(entity.getTypeInformation().getRawTypeInformation(), entity.getTypeAlias()); } }
@Nullable @Override public TypeInformation<?> resolveTypeFrom(Alias alias) { for (Entry<ClassTypeInformation<?>, Alias> entry : typeMap.entrySet()) { if (entry.getValue().hasSamePresentValueAs(alias)) { return entry.getKey(); } } for (PersistentEntity<?, ?> entity : mappingContext.getPersistentEntities()) { if (entity.getTypeAlias().hasSamePresentValueAs(alias)) { return entity.getTypeInformation().getRawTypeInformation(); } } return null; } }
public Alias createAliasFor(TypeInformation<?> type) { return typeMap.computeIfAbsent(type.getRawTypeInformation(), key -> { PersistentEntity<?, ?> entity = mappingContext.getPersistentEntity(key); if (entity == null || entity.getTypeAlias() == null) { return Alias.NONE; } return verify(key, entity.getTypeAlias()); }); }