From e35a0987992cc23ba06c6ddc15ae8a01d023bc84 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 16 Feb 2018 09:15:08 +0100 Subject: [PATCH] DATAJPA-658 - Fixed potential NullPointerException in JpaMetamodel. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When iterating over EntityType we need to guard against ….getJavaType() returning null. That seems to be the case for Hibernate Envers which apparently registers EntityType instances without a backing type. Original pull request: #146. --- .../data/jpa/util/JpaMetamodel.java | 2 +- .../data/jpa/util/JpaMetamodelUnitTests.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/jpa/util/JpaMetamodelUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/util/JpaMetamodel.java b/src/main/java/org/springframework/data/jpa/util/JpaMetamodel.java index 52860b5ab..3b3ee202e 100644 --- a/src/main/java/org/springframework/data/jpa/util/JpaMetamodel.java +++ b/src/main/java/org/springframework/data/jpa/util/JpaMetamodel.java @@ -76,7 +76,7 @@ public class JpaMetamodel { public boolean isSingleIdAttribute(Class entity, String name, Class attributeType) { return metamodel.getEntities().stream() // - .filter(it -> it.getJavaType().equals(entity)) // + .filter(it -> entity.equals(it.getJavaType())) // .findFirst() // .flatMap(it -> getSingularIdAttribute(it)) // .filter(it -> it.getJavaType().equals(attributeType)) // diff --git a/src/test/java/org/springframework/data/jpa/util/JpaMetamodelUnitTests.java b/src/test/java/org/springframework/data/jpa/util/JpaMetamodelUnitTests.java new file mode 100644 index 000000000..96e96fa50 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/util/JpaMetamodelUnitTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.util; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; + +import javax.persistence.metamodel.EntityType; +import javax.persistence.metamodel.Metamodel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +/** + * Unit tests for {@link JpaMetamodel}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class JpaMetamodelUnitTests { + + @Mock Metamodel metamodel; + + @Mock EntityType type; + + @Test + public void skipsEntityTypesWithoutJavaTypeForIdentifierLookup() { + + doReturn(Collections.singleton(type)).when(metamodel).getEntities(); + + assertThat(new JpaMetamodel(metamodel).isSingleIdAttribute(Object.class, "id", Object.class)).isFalse(); + } +}