org.hibernate.tool.hbm2ddl.DatabaseMetadata Java Examples

The following examples show how to use org.hibernate.tool.hbm2ddl.DatabaseMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalSessionFactoryBean.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute schema update script, determined by the Configuration object
 * used for creating the SessionFactory. A replacement for Hibernate's
 * SchemaUpdate class, for automatically executing schema update scripts
 * on application startup. Can also be invoked manually.
 * <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 #setSchemaUpdate
 * @see org.hibernate.cfg.Configuration#generateSchemaUpdateScript
 * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
 */
public void updateDatabaseSchema() throws DataAccessException {
	logger.info("Updating 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);
					String[] sql = getConfiguration().generateSchemaUpdateScript(dialect, metadata);
					executeSchemaScript(con, sql);
					return null;
				}
			}
		);
	}
	finally {
		if (dataSource != null) {
			configTimeDataSourceHolder.remove();
		}
	}
}
 
Example #2
Source File: LocalSessionFactoryBean.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
		}
	}
}
 
Example #3
Source File: LocalSessionFactoryBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Execute schema update script, determined by the Configuration object
 * used for creating the SessionFactory. A replacement for Hibernate's
 * SchemaUpdate class, for automatically executing schema update scripts
 * on application startup. Can also be invoked manually.
 * <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 #setSchemaUpdate
 * @see org.hibernate.cfg.Configuration#generateSchemaUpdateScript
 * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
 */
public void updateDatabaseSchema() throws DataAccessException {
	logger.info("Updating 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 {
					@SuppressWarnings("deprecation")
					Connection con = session.connection();
					DatabaseMetadata metadata = new DatabaseMetadata(con, dialect);
					String[] sql = getConfiguration().generateSchemaUpdateScript(dialect, metadata);
					executeSchemaScript(con, sql);
					return null;
				}
			}
		);
	}
	finally {
		if (dataSource != null) {
			configTimeDataSourceHolder.remove();
		}
	}
}
 
Example #4
Source File: LocalSessionFactoryBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 {
					@SuppressWarnings("deprecation")
					Connection con = session.connection();
					DatabaseMetadata metadata = new DatabaseMetadata(con, dialect, false);
					getConfiguration().validateSchema(dialect, metadata);
					return null;
				}
			}
		);
	}
	finally {
		if (dataSource != null) {
			configTimeDataSourceHolder.remove();
		}
	}
}
 
Example #5
Source File: Configuration.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)
		throws HibernateException {
	secondPassCompile();

	String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG );
	String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA );
	
	Iterator iter = getTableMappings();
	while ( iter.hasNext() ) {
		Table table = (Table) iter.next();
		if ( table.isPhysicalTable() ) {
			

			TableMetadata tableInfo = databaseMetadata.getTableMetadata(
					table.getName(),
					( table.getSchema() == null ) ? defaultSchema : table.getSchema(),
					( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(),
							table.isQuoted());
			if ( tableInfo == null ) {
				throw new HibernateException( "Missing table: " + table.getName() );
			}
			else {
				table.validateColumns( dialect, mapping, tableInfo );
			}

		}
	}

	iter = iterateGenerators( dialect );
	while ( iter.hasNext() ) {
		PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next();
		Object key = generator.generatorKey();
		if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) {
			throw new HibernateException( "Missing sequence or table: " + key );
		}
	}
}