[英]Find wether hibernate entity column(field member) is nullable at runtime
I have a problem where I would like to find that a field of a hibernate entity(by entityClass
) is representing nullable column or not. 我有一个问题要查找休眠实体(由entityClass
表示)的字段是否表示可为空的列。
However I want to get it using reliable hibernate meta not like checking for @NotNull
annotations and trying to cover any possible scenarios of hibernate considering an entity field nullable or not seems not very reliable. 但是我想使用可靠的休眠元数据来获取它,而不是像检查@NotNull
注释那样,并考虑实体字段为空或不是很可靠,试图涵盖任何可能的休眠方案。
Appreciate your kind help. 感谢您的帮助。
Thanks to @krokodilko I provide the solution code: 感谢@krokodilko,我提供了解决方案代码:
protected SessionFactory getSessionFactory() {
return getEntityManager().getEntityManagerFactory().unwrap(SessionFactory.class);
}
Get nullable metadata 获取可为空的元数据
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]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.