我正在尝试从hibernate 3迁移到5.2.3,并且有许多旧代码调用org.hibernate.SessionFactory.getAllClassMetadata
,这个方法在hibernate 5中已被弃用,它建议使用SessoinFactory.getMetaModel()
,但是当我尝试调用这个方法,它似乎也被弃用了,我继续研究,找不到任何其他建议使用的方法,任何人都可以帮忙吗?如果你能提供详细的用法,我们将非常感激,谢谢。
以下是getAllClassMetadata的用法:
@Override
public Collection<ModelSpecificationSummary> findAllModelSpecificationSummaries() throws RepositoryException {
List<ModelSpecificationSummary> summaries = new ArrayList<ModelSpecificationSummary>();
try {
Query query = session.createQuery(
"selecc.name, spec.class from " + Specification.class.getSimpleName() + " spec");
List<Object[]> list = query.list();
SingleTableEntityPersister metadata = (SingleTableEntityPersister) session.getSessionFactory()
.getAllClassMetadata()
.get(Specification.class.getName());
for (Object[] o : list) {
Long id = (Long) o[0];
String name = (String) o[1];
String discriminator = (String) o[2];
Class<? extends ModelSpecification> specificationClass = lookupSpecificationSubclass(metadata, discriminator);
ModelSpecificationSummary summary = new ModelSpecificationSummary(id, name, specificationClass);
summaries.add(summary);
}
return summaries;
}
catch (HibernateException he) {
throw new RepositoryException(he);
}
}
和lookupSpecificationSubclass类:
private Class<? extends ModelSpecification> lookupSpecificationSubclass(SingleTableEntityPersister metadata,
String discriminator)
throws RepositoryException {
Class<? extends ModelSpecification> specificationClass = null;
String subclass = metadata.getSubclassForDiscriminatorValue(discriminator);
if (subclass != null) {
try {
specificationClass = (Class<? extends ModelSpecification>) Class.forName(subclass);
}
catch (ClassNotFoundException e) {
throw new RepositoryException("Invalid class found for specification " + discriminator, e);
}
}
else {
throw new RepositoryException("No class found for specification " + discriminator);
}
return specificationClass;
}
所以我想如何用getAllClassMetadata()
EntityManagerFactory.getMetamodel()
答案 0 :(得分:0)
根据SessionFactory#getAllClassMetadata
文件:
已过时。使用
EntityManagerFactory.getMetamodel()
中的描述符代替
请注意,它建议使用未被弃用的EntityManagerFactory#getMetamodel
。您可以在网上找到许多解释如何使用它的示例,或者您可以询问具体问题,了解您希望用它实现的目标。