Home > OS >  Springboot Get if field is nullable on runtime
Springboot Get if field is nullable on runtime

Time:08-30

I'm making an application that presents different filters depending on the data you want to query. I'm trying to set if a filter is required or not based on its entities "nullable" property.

@Entity
@Table(name = "PLAYER")
public class Player {
    ...
    @Column(name = "NAME", nullable = false)
    private String name;
    ...
}

Then with an API call I get the fields and I want to check if a field is nullable or no, to mark it as required.

I have looked the documentation, but it just says what are the fields, it's options, how to use them, but nothing about checking the properties on runtime.

Can it be done?

CodePudding user response:

If I understood it correctly, you can make use of Java Reflections to check if annotation is set to nullable or not.

For example:

Field declaredField = Player.class.getDeclaredField("name");
        
Column annotation = declaredField.getAnnotation(Column.class);

System.out.println(annotation.nullable());
  • Related