method.invoke(parent, item);
return item;
} catch (NoSuchMethodException e) {
logger.debug("Method {} doesn't exist in the class {}", methodName, parent.getClass());
} catch (Exception e) {
throw new ActiveJpaException("Failed while invoking the method " + method.getName(), e);
}
// Try adding the item to the collection property returned by the getter
logger.trace("Attempting to invoke the getter for the property {} on the parent {}", name, parent);
Collection<T> collection = (Collection<T>) PropertyUtil.getProperty(parent, name);
if (collection != null) {
if (add) {
collection.add(item);
} else {
collection.remove(item);
}
return item;
}
// Try to find out the field and add/remove it to/from that
try {
logger.trace("Attempting to invoke the the property {} on the parent {}", name, parent);
Field field = parent.getClass().getDeclaredField(name);
field.setAccessible(true);
collection = (Collection<T>) field.get(parent);
if (add) {
collection.add(item);
} else {
collection.remove(item);
}
return item;
} catch (Exception e) {
logger.error("Failed to {} the item {} to the collection of the parent", add ? "add" : "remove", item, parent);
throw new ActiveJpaException("Failed while adding/removing the item to the collection - " + name, e);
}
}