miguelAngelOjeda / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao
0 stars 0 forks source link

JPAAnnotationMetadata only accepts direct public fields in getProps() #102

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
First, I'm so happy that the project is active again.

There is a bug in 
com.googlecode.genericdao.search.jpa.JPAAnnotationMetadata.getProps() which 
causes only direct public fields taken into consideration, so MappedSuperClass 
fields or protected fields of the parent object is ignored. Here is a fix to it:

1. Add the following method to JPAAnnotationMetadata:

    private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
        for (Field field : type.getDeclaredFields()) {
            fields.add(field);
        }

        if (type.getSuperclass() != null) {
            fields = getAllFields(fields, type.getSuperclass());
        }

        return fields;
    }

2. Replace this code:
        for (Field field : klass.getFields()) {
            if (null != field.getDeclaringClass().getAnnotation(Entity.class)
                    || null != field.getDeclaringClass().getAnnotation(Embeddable.class)) {
                props.put(field.getName(), new Property(field));
            }
        }

with this one:
        // genericdao has bug here. klass.getFields() only returns public fields, but all public
        // and protected inherited fields should be checked.
        List<Field> fields = getAllFields(new ArrayList<Field>(), klass);

        for (Field field : fields) {
            if (null != field.getDeclaringClass().getAnnotation(Entity.class)
                    || null != field.getDeclaringClass().getAnnotation(Embeddable.class)
                    || null != field.getDeclaringClass().getAnnotation(MappedSuperclass.class)) {
                props.put(field.getName(), new Property(field));
            }
        }

Thanks,
Mohsen

Original issue reported on code.google.com by mohs...@gmail.com on 29 Apr 2013 at 5:41