diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContext.java b/src/main/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContext.java index 4ea38a135..7fa79ceb8 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContext.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContext.java @@ -67,7 +67,8 @@ public class JpaMetamodelMappingContext */ @Override protected JpaPersistentEntityImpl createPersistentEntity(TypeInformation typeInformation) { - return new JpaPersistentEntityImpl(typeInformation, persistenceProvider, models.getMetamodel(typeInformation)); + return new JpaPersistentEntityImpl<>(typeInformation, persistenceProvider, + models.getRequiredMetamodel(typeInformation)); } /* @@ -91,17 +92,17 @@ public class JpaMetamodelMappingContext /** * We customize the lookup of {@link PersistentPropertyPaths} by also traversing properties that are embeddables. - * + * * @see org.springframework.data.mapping.context.AbstractMappingContext#findPersistentPropertyPaths(java.lang.Class, * java.util.function.Predicate) */ @Override public PersistentPropertyPaths findPersistentPropertyPaths(Class type, Predicate predicate) { - return doFindPersistentPropertyPaths(type, predicate, it -> it.isEmbeddable()); + return doFindPersistentPropertyPaths(type, predicate, JpaPersistentProperty::isEmbeddable); } - /* + /* * (non-Javadoc) * @see org.springframework.data.mapping.context.AbstractMappingContext#hasPersistentEntityFor(java.lang.Class) */ @@ -126,7 +127,7 @@ public class JpaMetamodelMappingContext /** * Returns the {@link JpaMetamodel} for the given type. - * + * * @param type must not be {@literal null}. * @return */ @@ -138,9 +139,29 @@ public class JpaMetamodelMappingContext return metamodel == null ? null : JpaMetamodel.of(metamodel); } + /** + * Returns the required {@link JpaMetamodel} for the given type or throw {@link IllegalArgumentException} if the + * {@code type} is not JPA-managed. + * + * @param type must not be {@literal null}. + * @return + * @throws IllegalArgumentException if {@code type} is not JPA-managed. + * @since 2.6.1 + */ + public JpaMetamodel getRequiredMetamodel(TypeInformation type) { + + JpaMetamodel metamodel = getMetamodel(type); + + if (metamodel == null) { + throw new IllegalArgumentException(String.format("Required JpaMetamodel not found for %s!", type)); + } + + return metamodel; + } + /** * Returns whether the given type is managed by one of the underlying {@link Metamodel} instances. - * + * * @param type must not be {@literal null}. * @return */ @@ -150,7 +171,7 @@ public class JpaMetamodelMappingContext /** * Returns whether the given type is managed by one of the underlying {@link Metamodel} instances. - * + * * @param type must not be {@literal null}. * @return */ diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContextIntegrationTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContextIntegrationTests.java index 81d4b0981..e09b9044c 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContextIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaMetamodelMappingContextIntegrationTests.java @@ -57,7 +57,7 @@ import org.springframework.transaction.support.TransactionTemplate; */ @ExtendWith(SpringExtension.class) @ContextConfiguration -public class JpaMetamodelMappingContextIntegrationTests { +class JpaMetamodelMappingContextIntegrationTests { private JpaMetamodelMappingContext context; @Autowired ProductRepository products;