Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Dec 18, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant