2

I am trying to migrate from hibernate 3 to 5.2.3, and have many old code calling org.hibernate.SessionFactory.getAllClassMetadata, this methods has been deprecated in hibernate 5, it suggests use SessoinFactory.getMetaModel(), but when I trying to call this method, it seems deprecated too, and I continue to research, cannot find any other method which suggested to use, anyone can help? if you can give a detail usage would be really appreciated, Thank you.

Here is the usage of the 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);
        }
    }

and the lookupSpecificationSubclass class:

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

so how am I suppose to replace getAllClassMetadata() with EntityManagerFactory.getMetamodel()

user2995031
  • 29
  • 1
  • 4
  • You can check this:: http://stackoverflow.com/questions/43604928/hibernate-upgrade-to-5-2-session-factory-creation-and-replacing-persistentclas/43718626 – Hasan K May 06 '17 at 20:09

1 Answers1

1

According to SessionFactory#getAllClassMetadata documentation:

Deprecated. Use the descriptors from EntityManagerFactory.getMetamodel() instead

Note that it suggests using EntityManagerFactory#getMetamodel which is not deprecated. You can find on the web many examples explaining how to use it or you can ask a specific question on what exactly you want to achieve with it.

acm
  • 2,086
  • 3
  • 16
  • 34
  • I'm trying to use EntityManagerFactory.getMetamodel() to replace getAllClassMetadata(), getAllClassMetadata() return a map with a key of string, but getMetamodel() return a set of entity, how should I match them up, thx – user2995031 Oct 18 '16 at 18:20
  • There is no way to exactly match the result returned by `SessionFactory#getAllClassMetadata` with the one returned by `EntityManagerFactory#getMetamodel`, you must analyze what do you need in each case and how can it be done using your new approach. I think you may find useful the following example http://stackoverflow.com/a/37695543/5078385. Additional examples: http://www.programcreek.com/java-api-examples/index.php?class=javax.persistence.EntityManagerFactory&method=getMetamodel. – acm Oct 18 '16 at 22:34
  • I just edited the question with adding the usage code – user2995031 Oct 19 '16 at 14:57