diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index a3945ab32..546e79c09 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2016 by the original author(s). + * Copyright 2011-2017 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -403,9 +403,7 @@ public class BasicPersistentEntity> implement public PersistentPropertyAccessor getPropertyAccessor(Object bean) { Assert.notNull(bean, "Target bean must not be null!"); - - Assert.isTrue(getType().isInstance(bean), - String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName())); + assertBeanType(bean); return new BeanWrapper(bean); } @@ -418,11 +416,18 @@ public class BasicPersistentEntity> implement public IdentifierAccessor getIdentifierAccessor(Object bean) { Assert.notNull(bean, "Target bean must not be null!"); - Assert.isTrue(getType().isInstance(bean), "Target bean is not of type of the persistent entity!"); + assertBeanType(bean); return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : NullReturningIdentifierAccessor.INSTANCE; } + private void assertBeanType(Object bean) { + + if (!getType().isInstance(bean)) { + throw new IllegalArgumentException(String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName())); + } + } + /** * A null-object implementation of {@link IdentifierAccessor} to be able to return an accessor for entities that do * not have an identifier property.