问题描述
因为 SessionFactory#getClassMetadata(java.lang.Class)
已过时,我无法使用 ClassMetadata .我想访问以下方法:
Since SessionFactory#getClassMetadata(java.lang.Class)
was deprecated, I am not able to use ClassMetadata. I would like to access to the following methods:
-
org.hibernate.metadata.ClassMetadata#getNaturalIdentifierProperties
-
org.hibernate.metadata.ClassMetadata#hasNaturalIdentifier
org.hibernate.metadata.ClassMetadata#getNaturalIdentifierProperties
org.hibernate.metadata.ClassMetadata#hasNaturalIdentifier
根据文档,我应该将EntityManagerFactory.getMetamodel()替换为getClassMetada.但是,元模型不包含获取natural-id的方法.我将xml映射用于natural-id,我想获取natural-id的属性名称以创建动态查询.
According to the documentation, I should replace getClassMetada with EntityManagerFactory.getMetamodel(). However, the metamodel does not contain methods to get natural-id. I am using xml mapping for natural-id and I would like to get the property names of the natural-id to create a dynamic query.
<class name="User">
<cache usage="read-write"/>
<id name="id">
<generator class="increment"/>
</id>
<natural-id>
<property name="name"/>
<property name="org"/>
</natural-id>
<property name="password"/>
</class>
- 有没有办法使用这些方法来获取自然的id映射?
- 还有另一种获取ClassMetadata实例的方法吗?
- 是否可以获得
entityMetamodel
替换ClassMetadata吗? - Is there a way to use those methods to get the natural id mapping?
- Is there another way to get a ClassMetadata instance?
- Is it possible to get an instance of
entityMetamodel
to replace the ClassMetadata?
推荐答案
好吧,知道已弃用SessionFactory#getClassMetadata(java.lang.Class)
,因此该选项正在使用sessionFactory.getMetamodel()
.进入休眠代码,这是我的解决方案:
Well, knowing that SessionFactory#getClassMetadata(java.lang.Class)
was deprecated, the option is using sessionFactory.getMetamodel()
. Checking into hibernate code, this was my solution:
MetamodelImplementor metamodel = (MetamodelImplementor) sessionFactory.getMetamodel();
ClassMetadata classMetadata = (ClassMetadata) metamodel.entityPersister(entityName);
首先,要知道的一个重要部分是什么是EntityName.它可以是映射实体的名称,也可以是Entity.class.getName().知道了,用SessionFactory#getClassMetadata(java.lang.Class)
替换应该获得类的名称并将其作为字符串传递.
First, one part important to know is what entityName is. It could be the name of the mapped entity or Entity.class.getName(). Knowing that, to replace SessionFactory#getClassMetadata(java.lang.Class)
with should get the name of the class and pass it as an string.
String entityName = EntityClass.class.getName();
第二,Hibernate具有JPA元模型的实现,名为MetamodelImplementor.此外,metamodel.entityPersister()返回EntityPersister接口. Hibernate使用AbstractEntityPersister实现了它.那是一个实现Lockable和ClassMetadata接口的抽象类.
Second, Hibernate has an implementation of JPA metamodel, named MetamodelImplementor.Additionally, metamodel.entityPersister() returns an EntityPersister interface. Hibernate implements it using AbstractEntityPersister. And that is an abstract class which implements Lockable and ClassMetadata interfaces.
ClassMetadata(接口)-> AbstractEntityPersister (抽象类)
ClassMetadata (interface) --> AbstractEntityPersister (abstract class)
因此,可以将AbstractEntityPersister强制转换为ClassMetadata.然后以这种方式返回相同的对象.
So, it is possible to cast AbstractEntityPersister to ClassMetadata. And in that way return same object.
这是SessionFactoryImpl
中的Hibernate代码的一部分:
This is a portion of code of Hibernate in SessionFactoryImpl
:
public ClassMetadata getClassMetadata(String entityName) throws HibernateException {
return (ClassMetadata) getMetamodel().entityPersister( entityName );
}
这篇关于Hibernate 5.2从元模型获取自然ID属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!