public HibernateProvider getHibernateProvider() { if (hibernateProvider == null) { hibernateProvider = new HibernateProvider(getConfiguration(), topiaServiceSupport, sessionRegistry, getImplementationClasses()); } return hibernateProvider; }
public AbstractTopiaApplicationContext(TopiaConfiguration configuration) { this.configuration = configuration; init(); }
protected void init() { new TopiaConfigurationBuilder().check(configuration); topiaFiresSupport = new TopiaFiresSupport(); sessionRegistry = new TopiaHibernateSessionRegistry(); // First initialize all the services initServices(); // ToPIA's schema init if (isInitSchema()) { if (log.isInfoEnabled()) { log.info("Schema initialization enabled"); } initSchema(); } else { if (log.isInfoEnabled()) { log.info("Schema initialization disabled"); } } // AThimel 14/06/14 Make sure this method is called AFTER ToPIA's schema init, otherwise Hibernate may have created the schema itself // The next line will trigger the Configuration#buildMappings() method which really initializes Hibernate getHibernateProvider().getHibernateConfiguration(); }
@Override public boolean isTableExists(Class<?> clazz) { // AThimel 14/06/14 getHibernateConfiguration() may create the schema, prefer using newHibernateConfiguration() which doesn't Configuration configuration = getHibernateProvider().getHibernateConfiguration(); Metadata metaData = getHibernateProvider().getMetaData(); boolean result = TopiaUtil.isSchemaExist(configuration, metaData, clazz.getName()); return result; }
getServices(TopiaMigrationService.class).values(); if (isSchemaEmpty()) { if (log.isInfoEnabled()) { log.info("schema is empty, will create"); createSchema(); if (migrationServiceEnabled) { if (log.isInfoEnabled()) {
@Override public boolean isOpened() { return ! isClosed(); } }
@Override public String getSchemaName() { // TODO AThimel 02/08/13 I absolutely don't know if it works return getConfiguration().getSchemaName(); }
@Override public List<Class<?>> getPersistenceClasses() { return new ArrayList<Class<?>>(getImplementationClasses()); }
for (TopiaService topiaService : getServices().values()) { topiaService.close();
@Override public boolean isSchemaEmpty() { // AThimel 14/06/14 getHibernateConfiguration() may create the schema, prefer using newHibernateConfiguration() which doesn't Configuration configuration = getHibernateProvider().newHibernateConfiguration(); configuration.getProperties().put(AvailableSettings.HBM2DDL_AUTO, "none"); SessionFactory sessionFactory = hibernateProvider.newSessionFactory(configuration); try { Metadata metaData = getHibernateProvider().newMetaData(configuration, sessionFactory); boolean result = TopiaUtil.isSchemaEmpty(configuration, metaData); return result; } finally { sessionFactory.close(); } }
protected TopiaIdFactory getTopiaIdFactory() { return getConfiguration().getTopiaIdFactory(); }
@Override public void showCreateSchema() { try { new SchemaExport().execute(EnumSet.of(TargetType.DATABASE, TargetType.STDOUT), SchemaExport.Action.CREATE, getHibernateProvider().getMetaData()); } catch (HibernateException eee) { throw new TopiaException(String.format("Could not show create schema for reason: %s", eee.getMessage()), eee); } }
/** * Detects if there is some schema existing for at least one of the dealed * entity of the underlying db context. * * @return {@code true} if there is no schema for any of the dealed entities, * {@code false} otherwise. * @since 2.5.3 */ protected boolean detectDbEmpty() { // XXX brendan 30/04/14 cast show a design smell HibernateProvider hibernateProvider = ((AbstractTopiaApplicationContext) applicationContext).getHibernateProvider(); // Get a new Configuration instance (not initialized) Configuration applicationHibernateConfiguration = hibernateProvider.newHibernateConfiguration(); applicationHibernateConfiguration.getProperties().remove(AvailableSettings.HBM2DDL_AUTO); // Make sure schema is not created by Hibernate boolean result = TopiaUtil.isSchemaEmpty(applicationHibernateConfiguration, metaData); return result; }
@Override public void createSchema() { try { EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.DATABASE); if (log.isDebugEnabled()) { targetTypes.add(TargetType.STDOUT); } topiaFiresSupport.firePreCreateSchema(this); Configuration configuration = getHibernateProvider().newHibernateConfiguration(); configuration.getProperties().remove(AvailableSettings.HBM2DDL_AUTO); SessionFactory sessionFactory = getHibernateProvider().newSessionFactory(configuration); try { Metadata metadata = getHibernateProvider().newMetaData(configuration, sessionFactory); new SchemaExport().execute(targetTypes, SchemaExport.Action.CREATE, metadata); topiaFiresSupport.firePostCreateSchema(this); } finally { sessionFactory.close(); } } catch (HibernateException eee) { throw new TopiaException(String.format("Could not create schema for reason: %s", eee.getMessage()), eee); } }
@Override public void dropSchema() { try { EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.DATABASE); if (log.isDebugEnabled()) { targetTypes.add(TargetType.STDOUT); } topiaFiresSupport.firePreDropSchema(this); new SchemaExport().execute(targetTypes, SchemaExport.Action.DROP, getHibernateProvider().getMetaData()); topiaFiresSupport.firePostDropSchema(this); } catch (HibernateException eee) { throw new TopiaException(String.format("Could not drop schema for reason: %s", eee.getMessage()), eee); } }
HibernateProvider hibernateProvider = ((AbstractTopiaApplicationContext) applicationContext).getHibernateProvider();
@Override public void updateSchema() { try { EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.DATABASE); if (log.isDebugEnabled()) { targetTypes.add(TargetType.STDOUT); } topiaFiresSupport.firePreUpdateSchema(this); new SchemaUpdate().execute(targetTypes, getHibernateProvider().getMetaData()); topiaFiresSupport.firePostUpdateSchema(this); } catch (HibernateException eee) { throw new TopiaException(String.format("Could not update schema for reason: %s", eee.getMessage()), eee); } }