我正在尝试从休眠3迁移到5.2.3,并且有许多调用org.hibernate.SessionFactory.getAllClassMetadata
的旧代码,该方法已在休眠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()
最佳答案
根据SessionFactory#getAllClassMetadata
文档:
不推荐使用。使用EntityManagerFactory.getMetamodel()
中的描述符
请注意,它建议使用不建议使用的EntityManagerFactory#getMetamodel
。您可以在网上找到许多说明如何使用它的示例,也可以提出一个具体的问题来确切地希望使用它实现什么。