/** * Extract the Configuration from the ServiceRegistry exposed by SessionFactoryImpl. Works in * Hibernate 4.3, but will probably break in a future version as they keep trying to make the * configuration harder to access (for some reason). Hopefully they will provide a interface to * get the full mapping data again. * * @param sessionFactory */ Configuration getConfigurationFromRegistry(SessionFactory sessionFactory) { ServiceRegistryImplementor serviceRegistry = ((SessionFactoryImplementor) _sessionFactory).getServiceRegistry(); SessionFactoryServiceRegistryImpl impl = (SessionFactoryServiceRegistryImpl) serviceRegistry; Configuration cfg = null; try { Field configurationField = SessionFactoryServiceRegistryImpl.class.getDeclaredField("configuration"); configurationField.setAccessible(true); Object configurationObject = configurationField.get(impl); cfg = (Configuration) configurationObject; } catch (Exception e) { e.printStackTrace(); } if (cfg == null) { throw new RuntimeException( "Unable to get the Configuration from the service registry. Please provide the Configuration in the constructor."); } return cfg; }
@Test public void generateCreateAnUpdateDDL() throws IOException { logger.debug("Generate create and update DDL"); EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) lcemfb.getNativeEntityManagerFactory(); SessionFactoryImpl sf = emf.getSessionFactory(); SessionFactoryServiceRegistryImpl serviceRegistry = (SessionFactoryServiceRegistryImpl) sf.getServiceRegistry(); Configuration cfg = null; try { Field field = SessionFactoryServiceRegistryImpl.class.getDeclaredField("configuration"); field.setAccessible(true); cfg = (Configuration) field.get(serviceRegistry); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } Files.createDirectories(Paths.get("target/db/migration/")); SchemaUpdate update = new SchemaUpdate(serviceRegistry, cfg); update.setDelimiter(";"); update.setOutputFile("target/db/migration/Vx__yy_zz.sql"); update.execute(false, false); SchemaExport export = new SchemaExport(serviceRegistry, cfg); export.setDelimiter(";"); export.setOutputFile("target/db/migration/create.sql"); export.execute(false, false, false, true); }
/** * Create the registry. * * @todo : fully expect this signature to change! * * @param sessionFactory The (in flux) session factory. Generally this is useful for grabbing a reference for later * use. However, care should be taken when invoking on the session factory until after it has been fully * initialized. * @param configuration The configuration object. * * @return The registry */ public SessionFactoryServiceRegistryImpl buildServiceRegistry( SessionFactoryImplementor sessionFactory, Configuration configuration);
/** * Create the registry. * * @todo : fully expect this signature to change! * * @param sessionFactory The (in flux) session factory. Generally this is useful for grabbing a reference for later * use. However, care should be taken when invoking on the session factory until after it has been fully * initialized. * @param metadata The configuration object. * * @return The registry */ public SessionFactoryServiceRegistryImpl buildServiceRegistry( SessionFactoryImplementor sessionFactory, MetadataImplementor metadata);