Annotation notation (using getAnnotation) doesn't work

I need to execute the following code to check if an object in mine has model

nullable=false

or similar annotation in the field.

import javax.persistence.Column;
import .....

private boolean isRequired(Item item, Object propertyId) {
        Class<?> property = getPropertyClass(item, propertyId);

        final JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
        if (null != joinAnnotation) {
            return !joinAnnotation.nullable();
        }

        final Column columnAnnotation = property.getAnnotation(Column.class);
        if (null != columnAnnotation) {
            return !columnAnnotation.nullable();
        }

        ....
        return false;
    }

      

Here is a snippet from my model.

import javax.persistence.*;
import .....

@Entity
@Table(name="m_contact_details")
public class MContactDetail extends AbstractMasterEntity implements Serializable {

    @Column(length=60, nullable=false)
    private String address1;

      

For those people who are not familiar with the annotation @Column

, here's the heading:

@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {

      

I would expect it to isRequired

return true

from time to time, but instead it never does. I have already followed mvn clean

and mvn install

in my project, but it doesn't help.

Q1: What am I doing wrong?

Q2: is there a cleaner coding way isRequired

(maybe it's better to use generics)?

+3


source to share


2 answers


  • property

    represents a class (this is a Class<?>

    )
  • @Column

    and @JoinColumn

    can only annotate fields / methods.

Hence, you will never find these annotations on property

.

A slightly modified version of your code that outputs whether the email property of the Employee object is required:



public static void main(String[] args) throws NoSuchFieldException {
      System.out.println(isRequired(Employee.class, "email"));
}

private static boolean isRequired(Class<?> entity, String propertyName) throws NoSuchFieldException {
    Field property = entity.getDeclaredField(propertyName);

    final JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
    if (null != joinAnnotation) {
        return !joinAnnotation.nullable();
    }

    final Column columnAnnotation = property.getAnnotation(Column.class);
    if (null != columnAnnotation) {
        return !columnAnnotation.nullable();
    }

    return false;
}

      

Please note that this is a semi-baked solution, because JPA annotations can be either field or method. Also keep in mind the difference between reflection methods like getFiled()

/ getDeclaredField()

. The former also returns inherited fields, while the latter only returns fields of a specific class, ignoring what is inherited from its parents.

+4


source


The following code works:



    @SuppressWarnings("rawtypes")
    private boolean isRequired(BeanItem item, Object propertyId) throws SecurityException {

        String fieldname = propertyId.toString();

        try {
            java.lang.reflect.Field field = item.getBean().getClass().getDeclaredField(fieldname);
            final JoinColumn joinAnnotation = field.getAnnotation(JoinColumn.class);
            if (null != joinAnnotation) {
                return !joinAnnotation.nullable();
            }

            final Column columnAnnotation = field.getAnnotation(Column.class);
            if (null != columnAnnotation) {
                return !columnAnnotation.nullable();
            }



        } catch (NoSuchFieldException e) {
            //not a problem no need to log this event.
            return false;
        } 
    }

      

0


source







All Articles