当前位置: 首页>>代码示例>>Java>>正文


Java DatabaseMetadata类代码示例

本文整理汇总了Java中org.hibernate.tool.hbm2ddl.DatabaseMetadata的典型用法代码示例。如果您正苦于以下问题:Java DatabaseMetadata类的具体用法?Java DatabaseMetadata怎么用?Java DatabaseMetadata使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DatabaseMetadata类属于org.hibernate.tool.hbm2ddl包,在下文中一共展示了DatabaseMetadata类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: updateDatabaseSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * 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();
		}
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:48,代码来源:LocalSessionFactoryBean.java

示例2: validateDatabaseSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * 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();
		}
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:45,代码来源:LocalSessionFactoryBean.java

示例3: validateSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
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 (key instanceof String) {
			key = normalizer.normalizeIdentifierQuoting( (String) key );
		}
		if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) {
			throw new HibernateException( "Missing sequence or table: " + key );
		}
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:40,代码来源:Configuration.java

示例4: generateDatabaseUpdateScript

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * Generates a <code>String</code> array with DML statements based on the Hibernate mapping files.
 *
 * @param configuration   The hibernate config, not null
 * @param session         The hibernate session, not null
 * @param databaseDialect The database dialect, not null
 * @return String[] array of DDL statements that were needed to keep the database in sync with the mapping file
 */
private static String[] generateDatabaseUpdateScript(Configuration configuration, Session session, Dialect databaseDialect) {
    try {
        DatabaseMetadata dbm = new DatabaseMetadata(session.connection(), databaseDialect);
        return configuration.generateSchemaUpdateScript(databaseDialect, dbm);
    } catch (SQLException e) {
        throw new UnitilsException("Could not retrieve database metadata", e);
    }
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:17,代码来源:HibernateAssert.java

示例5: validateSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
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 );
		}
	}
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:38,代码来源:Configuration.java

示例6: getData

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
private DatabaseMetadata getData() {
	try {
		Connection connection = ConventionUtils.getConnection(serviceRegistry);
		return new DatabaseMetadata(connection, dialect, configuration);
	} catch (SQLException e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:andreluiznsilva,项目名称:hibernate-conventions,代码行数:9,代码来源:DDLConventions.java

示例7: updateDatabaseSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * 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>() {
				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();
		}
	}
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:47,代码来源:LocalSessionFactoryBean.java

示例8: validateDatabaseSchema

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * 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>() {
				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();
		}
	}
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:44,代码来源:LocalSessionFactoryBean.java

示例9: updateCommand

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
protected static void updateCommand(String[] args) {
	String unitName, filename=null, url, username, password;
	if(args.length<5) System.out.println("Expected unitName jdbcUrl jdbcUsername jdbcPassword [filename]");
	else {
		unitName = args[1];
		url = args[2];
		username = args[3];
		password = args[4];
		if(args.length>5) filename = args[5];
		
		Configuration config = getConfiguration(unitName);
		Dialect dialect = Dialect.getDialect(config.getProperties());
		
		try {
			Connection conn = DriverManager.getConnection(url, username, password);
			
			DatabaseMetadata meta = new DatabaseMetadata(conn, dialect, config, true);

			List<SchemaUpdateScript> updateScriptList = config.generateSchemaUpdateScriptList(dialect, meta);
			String[] updateSQL = SchemaUpdateScript.toStringArray(updateScriptList);

			stringToStream(updateSQL, filename);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
 
开发者ID:premium-minds,项目名称:pm-persistence-utils,代码行数:28,代码来源:HibernateDDL.java

示例10: generateSchemaUpdateScript

import org.hibernate.tool.hbm2ddl.DatabaseMetadata; //导入依赖的package包/类
/**
 * @param dialect The dialect for which to generate the creation script
 * @param databaseMetadata The database catalog information for the database to be updated; needed to work out what
 * should be created/altered
 *
 * @return The sequence of DDL commands to apply the schema objects
 *
 * @throws HibernateException Generally indicates a problem calling {@link #buildMappings()}
 *
 * @see org.hibernate.tool.hbm2ddl.SchemaUpdate
 * 
 * @deprecated Use {@link #generateSchemaUpdateScriptList(Dialect, DatabaseMetadata)} instead
 */
@SuppressWarnings({ "unchecked" })
@Deprecated
public String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata databaseMetadata)
		throws HibernateException {
	List<SchemaUpdateScript> scripts = generateSchemaUpdateScriptList( dialect, databaseMetadata );
	return SchemaUpdateScript.toStringArray( scripts );
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:21,代码来源:Configuration.java


注:本文中的org.hibernate.tool.hbm2ddl.DatabaseMetadata类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。