Skip to content

Commit 78a727a

Browse files
committed
DATAJPA-664 - JpaPersistentProperty.getActualType() now considers special 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().
1 parent 851f1b2 commit 78a727a

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,11 +37,14 @@
3737
import javax.persistence.Version;
3838
import javax.persistence.metamodel.Metamodel;
3939

40+
import org.springframework.core.annotation.AnnotationUtils;
4041
import org.springframework.data.annotation.AccessType.Type;
4142
import org.springframework.data.mapping.Association;
4243
import org.springframework.data.mapping.PersistentEntity;
4344
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
4445
import org.springframework.data.mapping.model.SimpleTypeHolder;
46+
import org.springframework.data.util.ClassTypeInformation;
47+
import org.springframework.data.util.TypeInformation;
4548
import org.springframework.util.Assert;
4649

4750
/**
@@ -76,6 +79,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
7679

7780
private final Metamodel metamodel;
7881
private final Boolean usePropertyAccess;
82+
private final TypeInformation<?> associationTargetType;
7983

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

96100
this.metamodel = metamodel;
97101
this.usePropertyAccess = detectPropertyAccess();
102+
this.associationTargetType = isAssociation() ? detectAssociationTargetType() : null;
103+
}
104+
105+
/*
106+
* (non-Javadoc)
107+
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getActualType()
108+
*/
109+
@Override
110+
public Class<?> getActualType() {
111+
return associationTargetType == null ? super.getActualType() : associationTargetType.getType();
112+
}
113+
114+
/*
115+
* (non-Javadoc)
116+
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#getPersistentEntityType()
117+
*/
118+
@Override
119+
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
120+
return associationTargetType == null ? super.getPersistentEntityType() : Collections
121+
.singleton(associationTargetType);
98122
}
99123

100124
/*
@@ -215,4 +239,24 @@ private Boolean detectPropertyAccess() {
215239
access = findPropertyOrOwnerAnnotation(Access.class);
216240
return access == null ? null : AccessType.PROPERTY.equals(access.value());
217241
}
242+
243+
/**
244+
* Inspects the association annotations on the property and returns the target entity type if specified.
245+
*
246+
* @return
247+
*/
248+
private TypeInformation<?> detectAssociationTargetType() {
249+
250+
for (Class<? extends Annotation> associationAnnotation : ASSOCIATION_ANNOTATIONS) {
251+
252+
Annotation annotation = findAnnotation(associationAnnotation);
253+
Object targetEntity = AnnotationUtils.getValue(annotation, "targetEntity");
254+
255+
if (targetEntity != null && !void.class.equals(targetEntity)) {
256+
return ClassTypeInformation.from((Class<?>) targetEntity);
257+
}
258+
}
259+
260+
return null;
261+
}
218262
}

src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.mapping;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import java.util.Collections;
@@ -24,6 +24,7 @@
2424
import javax.persistence.AccessType;
2525
import javax.persistence.Embeddable;
2626
import javax.persistence.Embedded;
27+
import javax.persistence.ManyToOne;
2728
import javax.persistence.OneToOne;
2829
import javax.persistence.Transient;
2930
import javax.persistence.metamodel.Metamodel;
@@ -35,6 +36,8 @@
3536
import org.mockito.runners.MockitoJUnitRunner;
3637
import org.springframework.data.annotation.AccessType.Type;
3738
import org.springframework.data.annotation.Version;
39+
import org.springframework.data.util.ClassTypeInformation;
40+
import org.springframework.data.util.TypeInformation;
3841

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

167+
/**
168+
* @see DATAJPA-664
169+
*/
170+
@Test
171+
@SuppressWarnings("rawtypes")
172+
public void considersTargetEntityTypeForPropertyType() {
173+
174+
JpaPersistentProperty property = getProperty(SpecializedAssociation.class, "api");
175+
176+
assertThat(property.getType(), is(typeCompatibleWith(Api.class)));
177+
assertThat(property.getActualType(), is(typeCompatibleWith(Implementation.class)));
178+
179+
Iterable<? extends TypeInformation<?>> entityType = property.getPersistentEntityType();
180+
assertThat(entityType.iterator().hasNext(), is(true));
181+
assertThat(entityType.iterator().next(), is((TypeInformation) ClassTypeInformation.from(Implementation.class)));
182+
}
183+
164184
private JpaPersistentProperty getProperty(Class<?> ownerType, String propertyName) {
165185

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

271291
@Version long version;
272292
}
293+
294+
static class SpecializedAssociation {
295+
296+
@ManyToOne(targetEntity = Implementation.class) Api api;
297+
}
298+
299+
static interface Api {}
300+
301+
static class Implementation {}
273302
}

0 commit comments

Comments
 (0)