Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix problem with Object properties being used as one-to-many associat…

…ion ends.

Updated test to check this case.
  • Loading branch information...
commit 3648d27cea74d63745c12b7e73ace670608ab80d 1 parent 32f2cf7
@tomwidmer tomwidmer authored
View
26 ...ore-core/src/main/groovy/org/grails/datastore/mapping/model/config/GormMappingConfigurationStrategy.java
@@ -348,8 +348,8 @@ else if (!isPersistentEntity(relatedClassType)) {
String mappingProperty = (String)mappedByMap.get(property.getName());
if (StringUtils.hasText(mappingProperty)) {
// First find the specified property on the related class, if it exists.
- PropertyDescriptor pd = findProperty(referencedCpf.getPropertiesAssignableFromType(
- entity.getJavaClass()), mappingProperty);
+ PropertyDescriptor pd = findProperty(getPropertiesAssignableFromType(entity.getJavaClass(), referencedCpf),
+ mappingProperty);
// If a property of the required type does not exist, search
// for any collection properties on the related class.
@@ -404,7 +404,7 @@ else if (!isPersistentEntity(relatedClassType)) {
// otherwise figure out if there is a one-to-many relationship by retrieving any properties that are of the related type
// if there is more than one property then (for the moment) ignore the relationship
if (relatedClassPropertyType == null || Collection.class.isAssignableFrom(relatedClassPropertyType)) {
- List<PropertyDescriptor> descriptors = referencedCpf.getPropertiesAssignableFromType(entity.getJavaClass());
+ List<PropertyDescriptor> descriptors = getPropertiesAssignableFromType(entity.getJavaClass(), referencedCpf);
if (descriptors.size() == 1) {
final PropertyDescriptor pd = descriptors.get(0);
@@ -472,6 +472,18 @@ else if (Collection.class.isAssignableFrom(relatedClassPropertyType) ||
return association;
}
+ private List<PropertyDescriptor> getPropertiesAssignableFromType(Class type, ClassPropertyFetcher propertyFetcher) {
+ List<PropertyDescriptor> props = propertyFetcher.getPropertiesAssignableFromType(type);
+ // exclude properties of type object!
+ List<PropertyDescriptor> valid = new ArrayList<PropertyDescriptor>(props.size());
+ for (PropertyDescriptor prop : props) {
+ if (prop.getPropertyType() != null && !prop.getPropertyType().equals(Object.class)) {
+ valid.add(prop);
+ }
+ }
+ return valid;
+ }
+
private String findManyRelatedClassPropertyName(String propertyName,
ClassPropertyFetcher cpf, Map classRelationships, Class<?> classType) {
Map mappedBy = getMapStaticProperty(cpf, MAPPED_BY);
@@ -589,12 +601,12 @@ else if (descriptors.length > 1) {
// otherwise retrieve all the properties of the type from the associated class
if (relatedClassPropertyType == null) {
- PropertyDescriptor[] descriptors = ReflectionUtils.getPropertiesOfType(propType, entity.getJavaClass());
+ List<PropertyDescriptor> descriptors = getPropertiesAssignableFromType(entity.getJavaClass(), cpf);
// if there is only one then the association is established
- if (descriptors.length == 1) {
- relatedClassPropertyType = descriptors[0].getPropertyType();
- relatedClassPropertyName = descriptors[0].getName();
+ if (descriptors.size() == 1) {
+ relatedClassPropertyType = descriptors.get(0).getPropertyType();
+ relatedClassPropertyName = descriptors.get(0).getName();
}
}
}
View
1  grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/model/GormMappingSyntaxTests.groovy
@@ -172,6 +172,7 @@ class Author {
Long id
String name
Set books
+ def shouldBeIgnored
static hasMany = [books:Book]
}
Please sign in to comment.
Something went wrong with that request. Please try again.