javametadatahibernate-5.xmetamodelnaturalid

Hibernate 5.2 get natural id properties from metamodel


Since SessionFactory#getClassMetadata(java.lang.Class) was deprecated, I am not able to use ClassMetadata. I would like to access to the following methods:

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>

Solution

  • 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 );
    }