Skip to content

Commit

Permalink
DATAJPA-664 - JpaPersistentProperty.getActualType() now considers spe…
Browse files Browse the repository at this point in the history
…cial association type.

JPA mapping allows to define a specialized target entity type for associations e.g., to be able to use interfaces with associations but actually have them backed by a JPA entity type.

JpaPersistentPropertyImpl now favors a type detected within an association property over the one declared at the property for calls to getActualType().
  • Loading branch information
odrotbohm committed Jan 28, 2015
1 parent 851f1b2 commit 78a727a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -37,11 +37,14 @@
import javax.persistence.Version;
import javax.persistence.metamodel.Metamodel;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -76,6 +79,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer

private final Metamodel metamodel;
private final Boolean usePropertyAccess;
private final TypeInformation<?> associationTargetType;

/**
* Creates a new {@link JpaPersistentPropertyImpl}
Expand All @@ -95,6 +99,26 @@ public JpaPersistentPropertyImpl(Metamodel metamodel, Field field, PropertyDescr

this.metamodel = metamodel;
this.usePropertyAccess = detectPropertyAccess();
this.associationTargetType = isAssociation() ? detectAssociationTargetType() : null;
}

/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getActualType()
*/
@Override
public Class<?> getActualType() {
return associationTargetType == null ? super.getActualType() : associationTargetType.getType();
}

/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getPersistentEntityType()
*/
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
return associationTargetType == null ? super.getPersistentEntityType() : Collections
.singleton(associationTargetType);
}

/*
Expand Down Expand Up @@ -215,4 +239,24 @@ private Boolean detectPropertyAccess() {
access = findPropertyOrOwnerAnnotation(Access.class);
return access == null ? null : AccessType.PROPERTY.equals(access.value());
}

/**
* Inspects the association annotations on the property and returns the target entity type if specified.
*
* @return
*/
private TypeInformation<?> detectAssociationTargetType() {

for (Class<? extends Annotation> associationAnnotation : ASSOCIATION_ANNOTATIONS) {

Annotation annotation = findAnnotation(associationAnnotation);
Object targetEntity = AnnotationUtils.getValue(annotation, "targetEntity");

if (targetEntity != null && !void.class.equals(targetEntity)) {
return ClassTypeInformation.from((Class<?>) targetEntity);
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 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.
Expand All @@ -15,7 +15,7 @@
*/
package org.springframework.data.jpa.mapping;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.Collections;
Expand All @@ -24,6 +24,7 @@
import javax.persistence.AccessType;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.persistence.metamodel.Metamodel;
Expand All @@ -35,6 +36,8 @@
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.annotation.Version;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;

/**
* Unit tests for {@link JpaPersistentPropertyImpl}.
Expand Down Expand Up @@ -161,6 +164,23 @@ public void detectsSpringDataVersionAnnotation() {
assertThat(getProperty(SpringDataVersioned.class, "version").isVersionProperty(), is(true));
}

/**
* @see DATAJPA-664
*/
@Test
@SuppressWarnings("rawtypes")
public void considersTargetEntityTypeForPropertyType() {

JpaPersistentProperty property = getProperty(SpecializedAssociation.class, "api");

assertThat(property.getType(), is(typeCompatibleWith(Api.class)));
assertThat(property.getActualType(), is(typeCompatibleWith(Implementation.class)));

Iterable<? extends TypeInformation<?>> entityType = property.getPersistentEntityType();
assertThat(entityType.iterator().hasNext(), is(true));
assertThat(entityType.iterator().next(), is((TypeInformation) ClassTypeInformation.from(Implementation.class)));
}

private JpaPersistentProperty getProperty(Class<?> ownerType, String propertyName) {

JpaPersistentEntity<?> entity = context.getPersistentEntity(ownerType);
Expand Down Expand Up @@ -270,4 +290,13 @@ static class JpaVersioned {

@Version long version;
}

static class SpecializedAssociation {

@ManyToOne(targetEntity = Implementation.class) Api api;
}

static interface Api {}

static class Implementation {}
}

0 comments on commit 78a727a

Please sign in to comment.