/** * Execute schema creation script, determined by the Configuration object used for creating the * SessionFactory. A replacement for Hibernate's SchemaValidator class, to be invoked after * application startup. * * <p>Fetch the LocalSessionFactoryBean itself rather than the exposed SessionFactory to be able * to invoke this method, e.g. via {@code LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) * ctx.getBean("&mySessionFactory");}. * * <p>Uses the SessionFactory that this bean generates for accessing a JDBC connection to perform * the script. * * @throws DataAccessException in case of script execution errors * @see org.hibernate.cfg.Configuration#validateSchema * @see org.hibernate.tool.hbm2ddl.SchemaValidator */ public void validateDatabaseSchema() throws DataAccessException { logger.info("Validating database schema for Hibernate SessionFactory"); DataSource dataSource = getDataSource(); if (dataSource != null) { // Make given DataSource available for the schema update. configTimeDataSourceHolder.set(dataSource); } try { SessionFactory sessionFactory = getSessionFactory(); final Dialect dialect = ((SessionFactoryImplementor) sessionFactory).getDialect(); HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER); hibernateTemplate.execute( new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection con = session.connection(); DatabaseMetadata metadata = new DatabaseMetadata(con, dialect, false); getConfiguration().validateSchema(dialect, metadata); return null; } }); } finally { if (dataSource != null) { configTimeDataSourceHolder.remove(); } } }
/** * Execute schema creation script, determined by the Configuration object used for creating the * SessionFactory. A replacement for Hibernate's SchemaExport class, to be invoked on application * setup. * * <p>Fetch the LocalSessionFactoryBean itself rather than the exposed SessionFactory to be able * to invoke this method, e.g. via {@code LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) * ctx.getBean("&mySessionFactory");}. * * <p>Uses the SessionFactory that this bean generates for accessing a JDBC connection to perform * the script. * * @throws DataAccessException in case of script execution errors * @see org.hibernate.cfg.Configuration#generateSchemaCreationScript * @see org.hibernate.tool.hbm2ddl.SchemaExport#create */ public void createDatabaseSchema() throws DataAccessException { logger.info("Creating database schema for Hibernate SessionFactory"); DataSource dataSource = getDataSource(); if (dataSource != null) { // Make given DataSource available for the schema update. configTimeDataSourceHolder.set(dataSource); } try { SessionFactory sessionFactory = getSessionFactory(); final Dialect dialect = ((SessionFactoryImplementor) sessionFactory).getDialect(); HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); hibernateTemplate.execute( new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection con = session.connection(); String[] sql = getConfiguration().generateSchemaCreationScript(dialect); executeSchemaScript(con, sql); return null; } }); } finally { if (dataSource != null) { configTimeDataSourceHolder.remove(); } } }
/** * Execute schema drop script, determined by the Configuration object used for creating the * SessionFactory. A replacement for Hibernate's SchemaExport class, to be invoked on application * setup. * * <p>Fetch the LocalSessionFactoryBean itself rather than the exposed SessionFactory to be able * to invoke this method, e.g. via {@code LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) * ctx.getBean("&mySessionFactory");}. * * <p>Uses the SessionFactory that this bean generates for accessing a JDBC connection to perform * the script. * * @throws org.springframework.dao.DataAccessException in case of script execution errors * @see org.hibernate.cfg.Configuration#generateDropSchemaScript * @see org.hibernate.tool.hbm2ddl.SchemaExport#drop */ public void dropDatabaseSchema() throws DataAccessException { logger.info("Dropping database schema for Hibernate SessionFactory"); SessionFactory sessionFactory = getSessionFactory(); final Dialect dialect = ((SessionFactoryImplementor) sessionFactory).getDialect(); HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); hibernateTemplate.execute( new HibernateCallback<Object>() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Connection con = session.connection(); String[] sql = getConfiguration().generateDropSchemaScript(dialect); executeSchemaScript(con, sql); return null; } }); }