Announcement Announcement Module
Collapse
No announcement yet.
How to access annotated, private properties of jpa-entities. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to access annotated, private properties of jpa-entities.

    Hi there,

    in a lot of spring data tutorials, a bean property is private and annotated with @Id. Now I want to call the getProperty function of the @Id annotated property. BUT it could be inherited!

    java.lang.reflect.Field supports getAnnotations() but has no acces to inherited properties.

    and java.beans.Introspector does only support getProperty functions for access the annotations.

    So how can I get access to the private id value?

    This example fails cause getField throws NoSuchFieldException for inherited properties:

    Code:
    public K findIdField(final T entity) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException {
            
            final PropertyDescriptor[] descriptors = Introspector.getBeanInfo(entity.getClass()).getPropertyDescriptors();
            
            for (final PropertyDescriptor descriptor : descriptors) {
                
              Field field=  entity.getClass().getField(descriptor.getName());
                
                
                final Annotation[] annotations = field.getAnnotations();
                
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().equals(Id.class) || annotation.annotationType().equals(EmbeddedId.class)) {
                        @SuppressWarnings("unchecked")
                        final K value = (K) descriptor.getReadMethod().invoke(entity);
                        return value;
                    }
                }
            }
            return null;
        }

  • #2
    Not sure if this is best practice but it worked for me:
    Code:
     public K findIdField(final T entity, Class<?> superClass) throws IllegalAccessException, InvocationTargetException {
            
            Class<?> clazz;
            
            if (superClass == null) {
                clazz = entity.getClass();
            } else if (superClass.equals(Object.class)) {
                return null;
            } else {
                clazz = superClass;
            }
            
            final Field[] fields = clazz.getDeclaredFields();
            
            for (final Field field : fields) {
                
                final Annotation[] annotations = field.getAnnotations();
                
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().equals(Id.class) || annotation.annotationType().equals(EmbeddedId.class)) {
                        @SuppressWarnings("unchecked")
                        final K value = (K) BeanUtils.getPropertyDescriptor(clazz, field.getName()).getReadMethod().invoke(entity, null);
                        return value;
                    }
                }
            }
            
            return findIdField(entity, clazz.getSuperclass());
        }
    Last edited by h2cl; Jul 9th, 2012, 09:00 AM.

    Comment

    Working...
    X