Skip to content
Browse files

Merge branch '1.x'

  • Loading branch information...
2 parents 6f33c44 + 3648d27 commit 1058b90f86c6dad4f84602ebffdeccd333a2ff6a @tomwidmer tomwidmer committed
View
26 ...in/groovy/org/grails/datastore/mapping/model/config/GormMappingConfigurationStrategy.java
@@ -347,8 +347,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.
@@ -403,7 +403,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);
@@ -471,6 +471,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);
@@ -588,12 +600,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 ...ore-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]
}
View
13 ...store-gorm-mongo/src/test/groovy/org/grails/datastore/gorm/mongo/CascadeDeleteSpec.groovy
@@ -9,16 +9,19 @@ import spock.lang.Issue
class CascadeDeleteSpec extends GormDatastoreSpec {
- @Issue('GPMONGODB-187')
+ @Issue(['GPMONGODB-187', 'GPMONGODB-285'])
void "Test that a delete cascade from owner to child"() {
- given:"An owner with a child object "
+ expect:"No existing user settings"
+ CascadeUserSettings.findAll().isEmpty()
+
+ when:"An owner with a child object is saved"
def u = new CascadeUser(name:"user2")
def s = new CascadeUserSettings()
u.settings = [s] as Set
u.save(flush:true)
- when:"The owner is queried"
+ and:"The owner is queried"
def found1 = CascadeUser.findByName("user2")
def found1a = CascadeUserSettings.findByUser(found1)
@@ -29,11 +32,11 @@ class CascadeDeleteSpec extends GormDatastoreSpec {
when:"The owner is deleted"
found1.delete(flush:true)
def found2 = CascadeUser.findByName("user2")
- def found1b = CascadeUserSettings.findByUser(found1)
+ def allUserSettings = CascadeUserSettings.findAll()
then:"So is the child"
found2 == null
- found1b == null
+ allUserSettings.isEmpty()
}
@Override
View
14 ...mongo/src/main/groovy/org/grails/datastore/mapping/mongo/engine/MongoEntityPersister.java
@@ -33,6 +33,7 @@
import org.grails.datastore.mapping.engine.AssociationIndexer;
import org.grails.datastore.mapping.engine.EntityAccess;
import org.grails.datastore.mapping.engine.NativeEntryEntityPersister;
+import org.grails.datastore.mapping.engine.Persister;
import org.grails.datastore.mapping.engine.PropertyValueIndexer;
import org.grails.datastore.mapping.model.EmbeddedPersistentEntity;
import org.grails.datastore.mapping.model.MappingContext;
@@ -720,6 +721,19 @@ public Object doInDB(DB con) throws MongoException, DataAccessException {
});
}
+ @Override
+ protected void cascadeDeleteCollection(Collection collection) {
+ Persister persister = null;
+ for (Iterator iter = collection.iterator(); iter.hasNext(); ) {
+ Object child = iter.next();
+ if(persister == null) {
+ persister = session.getPersister(child);
+ }
+ persister.delete(child);
+ iter.remove();
+ }
+ }
+
protected DBObject createDBObjectWithKey(Object key) {
DBObject dbo = new BasicDBObject();
if (hasNumericalIdentifier || hasStringIdentifier) {

0 comments on commit 1058b90

Please sign in to comment.
Something went wrong with that request. Please try again.