How to get the All table metadata in spring boot - JPA - hibernate?

后端 未结 3 487
逝去的感伤
逝去的感伤 2021-01-14 04:09

I need to get META information of All the tables present in my schema dynamically , Meta infos are such as table , entity ,column name etc.

I have f

3条回答
  •  情歌与酒
    2021-01-14 04:59

    If you are using Springboot 1.5.x, The "HibernatePropertiesCustomizer" is not exist.

    I found a solution usable from here. You can not use integrator here, but you can add all the event listeners one by one. below is my code code:

    public class RootAwareInsertEventListener implements PersistEventListener {
    
        public static final RootAwareInsertEventListener INSTANCE = new RootAwareInsertEventListener();
    
        @Override
        public void onPersist(PersistEvent event) throws HibernateException {
            final Object entity = event.getObject();
    
            if (entity instanceof RootAware) {
                RootAware rootAware = (RootAware) entity;
                Object root = rootAware.getRoot();
                event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT);
    
                log.info("Incrementing {} entity version because a {} child entity has been inserted",
                        root, entity);
            }
        }
    
        @Override
        public void onPersist(PersistEvent event, Map createdAlready)
                throws HibernateException {
            onPersist(event);
        }
    }
    
    @Component
    public class HibernateListenerConfigurer {
    
        @PersistenceUnit
        private EntityManagerFactory emf;
    
        @PostConstruct
        protected void init() {
            SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
            EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
            registry.getEventListenerGroup(EventType.PERSIST).appendListener(RootAwareInsertEventListener.INSTANCE);
            registry.getEventListenerGroup(EventType.FLUSH_ENTITY).appendListener(RootAwareUpdateAndDeleteEventListener.INSTANCE);
    
        }
    }
    

提交回复
热议问题