JAVA如何通过反射获取不可为空的字段?

JAVA如何通过反射获取不可为空的字段?,java,hibernate,reflection,annotations,Java,Hibernate,Reflection,Annotations,我尝试从类(hibernate实体)中获取字段列表。像这样: 实体: public class A { public static final Integer someValue = 1; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "a_id") private Integer id; @ManyToOne(fetch = FetchType.

我尝试从类(hibernate实体)中获取字段列表。像这样:

实体:

public class A {

    public static final Integer someValue = 1;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "a_id")
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "a_b_id", nullable = true)
    private List<B> b;

    @NotNull
    @Column(name = "a_c_id")
    private C c;

    .........................
}
但我不知道如何使用参数检查字段的
@JoinColumn

我怎么做?有人能帮我吗?

对于注释
JoinColumn
,您只需首先使用获取它,然后检查
nullable()
的值

由此产生的过滤器可以是:

...
if (!Modifier.isStatic(field.getModifiers())) {
    // Add the field to the list only if it is annotated with NotNull
    // or annotated with JoinColumn and nullable is false
    JoinColumn jc;
    if (field.isAnnotationPresent(NotNull.class) || 
        ((jc = field.getAnnotation(JoinColumn.class)) != null && !jc.nullable())) {
        fieldsList.add(field);
    }
}
..

我将尝试这个解决方案。谢谢你的帮助!有没有办法访问这些字段的getter?如果我执行field.get(),它会抛出
java.lang.IllegalAccessException
,因为成员在实体中是私有的。有什么建议吗?@infinite如果是私人会员,您需要先让其可访问,请查看
field.isAnnotationPresent(NotNull.class)
...
if (!Modifier.isStatic(field.getModifiers())) {
    // Add the field to the list only if it is annotated with NotNull
    // or annotated with JoinColumn and nullable is false
    JoinColumn jc;
    if (field.isAnnotationPresent(NotNull.class) || 
        ((jc = field.getAnnotation(JoinColumn.class)) != null && !jc.nullable())) {
        fieldsList.add(field);
    }
}
..