From ae75f9528a17c43fbfba5bcc27e812c88b21a72f Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 4 Apr 2018 10:12:18 +0200 Subject: [PATCH] DATAJPA-1313 - Fix potential NullPointerException in JpaPersistentPropertyImpl. We turned around comparison order to prevent null dereference when EntityType.getJavaType() returns null. EntityType.getJavaType() may return null values if Hibernate reports dynamic models or an entity class is mapped multiple times. That seems to be the case for Hibernate Envers which apparently registers EntityType instances without a backing type. --- .../jpa/mapping/JpaPersistentPropertyImpl.java | 5 +++-- .../JpaPersistentPropertyImplUnitTests.java | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java index c4da5e6da..d7f7cc364 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,6 +58,7 @@ import org.springframework.util.Assert; * @author Oliver Gierke * @author Thomas Darimont * @author Greg Turnquist + * @author Mark Paluch * @since 1.3 */ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty @@ -133,7 +134,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty ownerEntityType = null; for (EntityType entityType : metamodel.getEntities()) { - if (entityType.getJavaType().equals(type)) { + if (type.equals(entityType.getJavaType())) { ownerEntityType = entityType; break; } diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java index c0c5965af..394272f5c 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import javax.persistence.Embedded; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Transient; +import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.Metamodel; @@ -44,9 +45,10 @@ import org.springframework.data.util.TypeInformation; /** * Unit tests for {@link JpaPersistentPropertyImpl}. - * + * * @author Oliver Gierke * @author Greg Turnquist + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class JpaPersistentPropertyImplUnitTests { @@ -156,6 +158,15 @@ public class JpaPersistentPropertyImplUnitTests { assertThat(getProperty(Sample.class, "other").isEntity(), is(false)); } + @Test // DATAJPA-1313 + public void dynamicEntityTypeDoesNotFail() { + + EntityType managedType = mock(EntityType.class); + doReturn(Collections.singleton(managedType)).when(model).getEntities(); + + assertThat(context.getPersistentEntity(WithReadOnly.class), is(notNullValue())); + } + private JpaPersistentProperty getProperty(Class ownerType, String propertyName) { JpaPersistentEntity entity = context.getPersistentEntity(ownerType);