DATAJPA-658 - Fixed potential NullPointerException in JpaMetamodel.

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.
This commit is contained in:
Oliver Gierke
2018-02-16 09:15:08 +01:00
parent 1f5e757448
commit e35a098799
2 changed files with 51 additions and 1 deletions

View File

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

View File

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