/** * Get the current Hibernate Configuration. The Configuration instance is lazy-initialized using the * {@link #newHibernateConfiguration()} method. The returned instance is always initialized and mapping are built. * * @return the Hibernate Configuration instance with built mappings. */ public Configuration getHibernateConfiguration() { if (hibernateConfiguration == null) { hibernateConfiguration = newHibernateConfiguration(); TopiaUtil.warnOnAutomaticSchemaOperationRisk(hibernateConfiguration); } return hibernateConfiguration; }
/** * 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 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(); } }
@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); } }