Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

java - Hibernate upgrade to 5.2 - Session Factory creation and replacing PersistentClass for getting entity class properties

I am currently upgrading my Hibernate version to the latest version 5.2.10. I replaced my code in the HibernateUtil for the SessionFactory creation.

4.3.11.Final (Previous):

 public class HibernateUtil {
   private HibernateUtil() {}

   private static SessionFactory sessionFactory;

    private static Configuration configuration;

    public static Configuration getConfiguration() {
        return configuration;
    }
    private static SessionFactory buildSessionFactory() {
        try {
                     if(sessionFactory == null) {
                        configuration = new Configuration();
                        configuration.configure("hibernate.cfg.xml");
                        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                                .applySettings(configuration.getProperties()).build();
                        sessionFactory = configuration
                                .buildSessionFactory(serviceRegistry);
                     }
            return sessionFactory;
        }
        catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

    }
    public static SessionFactory getSessionFactory() {
        return buildSessionFactory();
    }

    public static Session getSession() {
        Session hibernateSession = getSessionFactory().getCurrentSession();
        return hibernateSession;
      }

    public static void shutdown() {
       getSessionFactory().close();
    }
}

5.2.10 Final (New):

public class HibernateUtil {
  private static StandardServiceRegistry registry;
  private static SessionFactory sessionFactory;

   public static SessionFactory getSessionFactory() {
        return buildSessionFactory();
   }

  public static SessionFactory buildSessionFactory() {
    if (sessionFactory == null) {
      try {
        registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        MetadataSources sources = new MetadataSources(registry);
        Metadata metadata = sources.getMetadataBuilder().build();
        sessionFactory = metadata.getSessionFactoryBuilder().build();
      } catch (Exception e) {
        e.printStackTrace();
        shutdown();
      }
    }
    return sessionFactory;
  }

public static Session getSession() {
        Session hibernateSession = getSessionFactory().getCurrentSession();
        return hibernateSession;
      }

  public static void shutdown() {
    if (registry != null) {
      StandardServiceRegistryBuilder.destroy(registry);
    }
  }
}

Now I have a method which would fetch me the list of column names by passing the DB table name as a string. I did it like this before in 4.3.11.Final:

public static List<String> getColumnNames(String tableName) {

        List<String> columnList=null;

        Map<String, ClassMetadata> map = HibernateUtil.getSessionFactory().getAllClassMetadata();
        Iterator<Entry<String, ClassMetadata>>  itr =  map.entrySet().iterator();

        while(itr.hasNext()){

            ClassMetadata classMetaData = itr.next().getValue();
            AbstractEntityPersister aep = (AbstractEntityPersister) classMetaData;

            if(aep.getTableName().split("\.")[1].equalsIgnoreCase(tableName)){

                columnList = new ArrayList<String>();
                String[] propertyNames = classMetaData.getPropertyNames();

                for(String property : propertyNames){
                        try {
                            PersistentClass persistentClass = HibernateUtil .getConfiguration().getClassMapping(classMetaData.getEntityName());
                            String clmName =  ((Column) persistentClass.getProperty(property).getColumnIterator().next()).getName();
                            columnList.add(clmName);
                        } catch(NoSuchElementException e){
                            log.error("Element not found idenfied as : "+property);
                        } catch(Exception e){
                            log.error(e.getMessage());
                        }
                }
                break;
            }
        }

        return columnList;
    }

Now after the upgrade it shows the method getAllClassMetadata as deprecated and am facing difficulty to get the PersistentClass object. I saw a similar question here but I couldn't exactly figure out the solution. What part of my current code do I have to change for my getColumnNames() method to work exactly like before. I referred the documentation and it says to use the EntityManagerFactory.getMetamodel() instead but i can't find suitable reference examples of the same. Also will I have to change the SessionFactory creation mechanism for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, we need to create a new MetadataExtractorIntegrator class that implements the Hibernate Integrator:

public class MetadataExtractorIntegrator 
    implements org.hibernate.integrator.spi.Integrator {

    public static final MetadataExtractorIntegrator INSTANCE = 
        new MetadataExtractorIntegrator();

    private Database database;

    @Override
    public void integrate(
            Metadata metadata,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {

        database = metadata.getDatabase();
    }

    @Override
    public void disintegrate(
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    }

    public Database getDatabase() {
        return database;
    }
}

Then, we can just configure Hibernate to use it.

If you're using the Hibernate bootstrap mechanism, then you can add it like this:

final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder();
bsrb.enableAutoClose();

Integrator integrator = integrator();
if (integrator != null) {
    bsrb.applyIntegrator( integrator );
}

final BootstrapServiceRegistry bsr = bsrb.build();

final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr);

If you're bootstrapping with JPA, then you can do it as follows:

protected EntityManagerFactory newEntityManagerFactory() {
    PersistenceUnitInfo persistenceUnitInfo = persistenceUnitInfo(
        getClass().getSimpleName()
    );
    
    Map<String, Object> configuration = new HashMap<>();
    
    
    configuration.put("hibernate.integrator_provider", 
        (IntegratorProvider) () -> Collections.singletonList( MetadataExtractorIntegrator.INSTANCE )
    );

    EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = new EntityManagerFactoryBuilderImpl(
            new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration
    );
    return entityManagerFactoryBuilder.build();
}

Now, when running the following test:

for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE
    .getDatabase()
    .getNamespaces()) {
     
    for( Table table : namespace.getTables()) {
        LOGGER.info( "Table {} has the following columns: {}",
             table,
             StreamSupport.stream(
                Spliterators.spliteratorUnknownSize( 
                    table.getColumnIterator(), 
                    Spliterator.ORDERED
                ), 
                false
            )
            .collect( Collectors.toList()) 
        );
    }
}

Hibernate outputs all the currently mapped tables in the log:

Table org.hibernate.mapping.Table(post) has the following columns: [
    org.hibernate.mapping.Column(id), 
    org.hibernate.mapping.Column(title), 
    org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_comment) has the following columns: [
    org.hibernate.mapping.Column(id), 
    org.hibernate.mapping.Column(review), 
    org.hibernate.mapping.Column(version), 
    org.hibernate.mapping.Column(post_id)
]
Table org.hibernate.mapping.Table(post_details) has the following columns: [
    org.hibernate.mapping.Column(id), 
    org.hibernate.mapping.Column(created_by), 
    org.hibernate.mapping.Column(created_on), 
    org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_tag) has the following columns: [
    org.hibernate.mapping.Column(post_id), 
    org.hibernate.mapping.Column(tag_id)
]
Table org.hibernate.mapping.Table(tag) has the following columns: [
    org.hibernate.mapping.Column(id), 
    org.hibernate.mapping.Column(name), 
    org.hibernate.mapping.Column(version)
]

That's it!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...