EntityIdPropertyResolver implements IdPropertyResolver { @Override public String resolve(Class<?> entityType, Class<?> idType) { log.info("Trying to resolve the ID property for entity " + entityType + " using the annotated getter method"); String idProperty = annotatedGetterIdPropertyResolver.resolve(entityType, idType); if (idProperty == null) { log.info("Trying to resolve the ID property for entity " + entityType + " using the annotated ID field"); idProperty = annotatedFieldIdPropertyResolver.resolve(entityType, idType); } if (idProperty == null) { log.info("Trying to resolve the ID property for entity " + entityType + " using the getter method"); idProperty = namedGetterIdPropertyResolver.resolve(entityType, idType); } if (idProperty == null) { log.info("Trying to resolve the ID property for entity " + entityType + " using the field"); idProperty = namedFieldIdPropertyResolver.resolve(entityType, idType); } if (idProperty == null) { log.error("No ID property was found for entity " + entityType); throw new NoIdPropertyException(entityType); } final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(entityType, StringUtils.capitalize(idProperty)); if (descriptor.getType().isPrimitive()) { throw new PrimitiveIdTypeException(entityType, idProperty); } return idProperty; }  @Override String resolve(Class<?> entityType, Class<?> idType);  }
@Test public void testThatAnnotatedGetterHasPrecedence() { final String resolved = resolver.resolve(EntityWithAnnotationOnIdFieldAndGetterAndAnIdField.class, Object.class); assertThat(resolved, is(notNullValue())); assertThat(resolved, is("unannotatedId")); }
@Test public void testThatAnnotatedPropertyIsSecond() { final String resolved = resolver.resolve(EntityWithIdFieldAndAnAnnotatedIdField.class, Object.class); assertThat(resolved, is(notNullValue())); assertThat(resolved, is("annotatedId")); }
@Test public void testThatNamedGetterIsThird() { final String resolved = resolver.resolve(EntityWithUnderscorePrecedingIdField.class, Object.class); assertThat(resolved, is(notNullValue())); assertThat(resolved, is("id")); }
@Test public void testThatNamedFieldIsFourth() { final String resolved = resolver.resolve(EntityWithIdFieldHiddenBehindDifferentlyNamedAccessors.class, Object.class); assertThat(resolved, is(notNullValue())); assertThat(resolved, is("id")); }
@Test(expectedExceptions = NoIdPropertyException.class) public void testThatNoOtherValueIsHonored() { resolver.resolve(EntityWithNoImmediatelyResolvableIdProperty.class, Object.class); }
@Test(expectedExceptions = PrimitiveIdTypeException.class) public void testPrimitiveIdTypeDoesNotWork() { resolver.resolve(EntityWithPrimitiveIdProperty.class, Long.class); }