Hibernate 5.2 get natural id properties from metamodel

醉酒当歌 提交于 2019-12-11 01:49:00

问题


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>
  • 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?

回答1:


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


来源:https://stackoverflow.com/questions/43499887/hibernate-5-2-get-natural-id-properties-from-metamodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!