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.
This commit is contained in:
Mark Paluch
2018-04-04 10:12:18 +02:00
parent 240fad23f1
commit ae75f9528a
2 changed files with 16 additions and 4 deletions

View File

@@ -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<JpaPersistentProperty>
@@ -133,7 +134,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
EntityType<?> ownerEntityType = null;
for (EntityType<?> entityType : metamodel.getEntities()) {
if (entityType.getJavaType().equals(type)) {
if (type.equals(entityType.getJavaType())) {
ownerEntityType = entityType;
break;
}

View File

@@ -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);