查找 hibernate 实体列(字段成员)在运行时是否可为空

Find wether hibernate entity column(field member) is nullable at runtime

我有一个问题,我想找到一个休眠实体的字段(通过 entityClass)是否代表可为空的列。

但是我想使用可靠的 hibernate meta 来获取它,而不是像检查 @NotNull 注释那样尝试覆盖任何可能的 hibernate 场景,考虑到实体字段是否可为 null 似乎不太可靠。

感谢您的帮助。

感谢@krokodilko我提供解决方案代码:

protected SessionFactory getSessionFactory() {
    return getEntityManager().getEntityManagerFactory().unwrap(SessionFactory.class);
}

获取可为空的元数据

Map<String, Boolean> entityPropNullables = new HashMap<String, Boolean>();
// Provide the class of disired metadata as parameter
ClassMetadata entityMeta = getSessionFactory().getClassMetadata(anEntityClass);
String[] propNames = entityMeta.getPropertyNames();
boolean[] isNullableProps = entityMeta.getPropertyNullability();

if (propNames != null) {
    for (int i = 0; i < propNames.length; i++ ) {
        entityPropNullables.put(propNames[i], isNullableProps[i]);
    }
}