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
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>
entityMetamodel
to replace the ClassMetadata?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);
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();
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.
EntityPersister (interface) --> Lockable (interface) --> AbstractEntityPersister (abstract class)
ClassMetadata (interface) --> AbstractEntityPersister (abstract class)
So, it is possible to cast AbstractEntityPersister to ClassMetadata. And in that way return same object.
This is a portion of code of Hibernate in SessionFactoryImpl
:
public ClassMetadata getClassMetadata(String entityName) throws HibernateException {
return (ClassMetadata) getMetamodel().entityPersister( entityName );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With