this.checkOrderByAnnotation(field, orderBy);
if (field.getType() != List.class) {
throw new UnsupportedFieldTypeException("The annotated field " + field.getName() + " in " + beanClass + " is of unsupported type "
+ field.getType() + ". Fields annotated as @OneToMany have to be a genericly typed " + List.class, field, OneToMany.class,
beanClass);
}
if (ReflectionUtils.getGenericlyTypeCount(field) != 1) {
throw new UnsupportedFieldTypeException("The annotated field " + field.getName() + " in " + beanClass
+ " is genericly typed. Fields annotated as @OneToMany have to be a genericly typed " + List.class, field, OneToMany.class,
beanClass);
}
// This is a bit ugly but still necessary until someone else
// comes up with something smarter...
Class<?> remoteClass = (Class<?>) ReflectionUtils.getGenericType(field);
SimplifiedRelation simplifiedRelation = field.getAnnotation(SimplifiedRelation.class);
if (simplifiedRelation != null) {
this.oneToManyRelations.put(field, SimplifiedOneToManyRelation.getGenericInstance(beanClass, remoteClass, field, this, typePopulators, queryParameterPopulators));
} else {
// Use this class pks, no extra field
this.oneToManyRelations.put(field, DefaultOneToManyRelation.getGenericInstance(beanClass, remoteClass, field, daoFactory, daoManaged));
}
if (oneToMany.autoAdd()) {
this.autoAddRelations.add(field);
}
if (oneToMany.autoUpdate()) {
this.autoUpdateRelations.add(field);
}
if (oneToMany.autoGet()) {
this.autoGetRelations.add(field);
}
} else if (field.isAnnotationPresent(ManyToOne.class)) {
this.checkAutoGeneration(daoManaged);
ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
List<Field> remoteClassFields = ReflectionUtils.getFields(field.getType());
Field matchingRemoteField = null;
if (remoteClassFields != null) {
for (Field remoteField : remoteClassFields) {
if (remoteField.isAnnotationPresent(DAOManaged.class) && remoteField.isAnnotationPresent(OneToMany.class)
&& remoteField.getType() == List.class && ReflectionUtils.isGenericlyTyped(remoteField)
&& ((Class<?>) ReflectionUtils.getGenericType(remoteField) == this.beanClass)) {
matchingRemoteField = remoteField;
break;
}
}
}
if (matchingRemoteField == null) {
throw new RuntimeException("No corresponding @OneToMany annotated field found in " + field.getType()
+ " matching @ManyToOne relation of field " + field.getName() + " in " + beanClass + "!");
}
Field remoteKeyField = null;
if (!StringUtils.isEmpty(manyToOne.remoteKeyField())) {
remoteKeyField = ReflectionUtils.getField(field.getType(), manyToOne.remoteKeyField());
//TODO Check if the remote key field is @DAOPopluate annotated
if (remoteKeyField == null) {
throw new RuntimeException("Unable to find @Key annotated field " + manyToOne.remoteKeyField() + " in " + field.getType() + " specified for @ManyToOne annotated field "
+ field.getName() + " in " + beanClass);
}
} else {
for (Field remoteField : remoteClassFields) {
if (remoteField.isAnnotationPresent(DAOManaged.class) && remoteField.isAnnotationPresent(Key.class)) {
if (remoteKeyField != null) {
throw new RuntimeException("Found multiple @Key annotated fields in " + field.getType() + ", therefore the remoteKeyField property needs to be specified for the @ManyToOne annotated field "
+ field.getName() + " in " + beanClass);
}
remoteKeyField = remoteField;
}
}
if (remoteKeyField == null) {
throw new RuntimeException("Unable to find @Key annotated field in " + field.getType() + " while parsing @ManyToOne annotated field "
+ field.getName() + " in " + beanClass);
}
}
DefaultManyToOneRelation<T, ?, ?> relation = null;
if (field.isAnnotationPresent(Key.class)) {
relation = DefaultManyToOneRelation.getGenericInstance(beanClass, field.getType(), remoteKeyField.getType(), field, remoteKeyField, daoManaged, daoFactory);
manyToOneRelationKeys.put(field, relation);
} else {
relation = DefaultManyToOneRelation.getGenericInstance(beanClass, field.getType(), remoteKeyField.getType(), field, remoteKeyField, daoManaged, daoFactory);
this.manyToOneRelations.put(field, relation);
}
this.columnMap.put(field, relation);
if (orderBy != null) {
this.columnOrderMap.put(orderBy, relation);
}
if (manyToOne.autoAdd()) {
this.autoAddRelations.add(field);
}
if (manyToOne.autoUpdate()) {
this.autoUpdateRelations.add(field);
}
if (manyToOne.autoGet()) {
this.autoGetRelations.add(field);
}
} else if (field.isAnnotationPresent(ManyToMany.class)) {
this.checkAutoGeneration(daoManaged);
this.checkOrderByAnnotation(field, orderBy);
if (field.getType() != List.class) {
throw new UnsupportedFieldTypeException("The annotated field " + field.getName() + " in " + beanClass + " is of unsupported type "
+ field.getType() + ". Fields annotated as @ManyToMany have to be a genericly typed " + List.class, field, ManyToMany.class,
beanClass);
}
if (ReflectionUtils.getGenericlyTypeCount(field) != 1) {
throw new UnsupportedFieldTypeException("The annotated field " + field.getName() + " in " + beanClass
+ " is genericly typed. Fields annotated as @ManyToMany have to be a genericly typed " + List.class, field, ManyToMany.class,
beanClass);
}
// This is a bit ugly but still necessary until someone else