List of usage examples for org.hibernate.mapping ForeignKey sqlCreateString
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema)
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void transferForeignKeys(Connection connection, Table fromTable, Table toTable) throws SQLException { DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); Iterator i = config.getTableMappings(); while (i.hasNext()) { Table t = (Table) i.next();//from www . ja v a 2 s . c om Iterator fki = t.getForeignKeyIterator(); while (fki.hasNext()) { ForeignKey fk = (ForeignKey) fki.next(); Table referencedTable = fk.getReferencedTable(); if (referencedTable != null && referencedTable == fromTable) { TableMetadata tableInfo = meta.getTableMetadata(t.getName(), defaultSchema, defaultCatalog, false); // verify that the fk is actually in the database. if (tableInfo.getForeignKeyMetadata(fk.getName()) == null) { // foreign key does not exist, so do not drop or recreate it. continue; } String sql = fk.sqlDropString(dialect, defaultCatalog, defaultSchema); LOG.info(sql); JDBCUtils.execute(connection, sql); fk.setReferencedTable(toTable); sql = fk.sqlCreateString(dialect, null, defaultCatalog, defaultSchema); LOG.info(sql); JDBCUtils.execute(connection, sql); } } } }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void recreatedDroppedConstraints(List<ForeignKey> droppedConstraints, Connection connection) throws SQLException { for (ForeignKey columnKey : droppedConstraints) { String sql = columnKey.sqlCreateString(dialect, config.getMapping(), defaultCatalog, defaultSchema); LOG.info(sql);// ww w. j a v a 2 s. c om JDBCUtils.execute(connection, sql); } }
From source file:org.apereo.portal.tools.dbloader.HibernateDbLoader.java
License:Apache License
/** Generate create scripts and add them to the script list */ @SuppressWarnings("unchecked") protected List<String> createScript(Collection<Table> tables, Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema) { final List<String> script = new ArrayList<String>(tables.size() * 2); for (final Table table : tables) { if (table.isPhysicalTable()) { script.add(table.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); }/*from www. j a v a2 s .co m*/ } for (final Table table : tables) { if (table.isPhysicalTable()) { if (!dialect.supportsUniqueConstraintInCreateAlterTable()) { for (final Iterator<UniqueKey> subIter = table.getUniqueKeyIterator(); subIter.hasNext();) { final UniqueKey uk = subIter.next(); final String constraintString = uk.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema); if (constraintString != null) { script.add(constraintString); } } } for (final Iterator<Index> subIter = table.getIndexIterator(); subIter.hasNext();) { final Index index = subIter.next(); script.add(index.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } if (dialect.hasAlterTable()) { for (final Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); subIter.hasNext();) { final ForeignKey fk = subIter.next(); if (fk.isPhysicalConstraint()) { script.add(fk.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } } } } } return script; }
From source file:org.beangle.orm.hibernate.tool.DdlGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private void generateTableSql(Table table) { if (!table.isPhysicalTable()) return;/*from w w w .ja va 2s . c o m*/ Iterator<String> commentIter = table.sqlCommentStrings(dialect, defaultCatalog, defaultSchema); while (commentIter.hasNext()) { comments.add(commentIter.next()); } if (processed.contains(table)) return; processed.add(table); tables.add(table.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); Iterator<UniqueKey> subIter = table.getUniqueKeyIterator(); while (subIter.hasNext()) { UniqueKey uk = subIter.next(); String constraintString = uk.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema); if (constraintString != null) constraints.add(constraintString); } Iterator<Index> idxIter = table.getIndexIterator(); while (idxIter.hasNext()) { final Index index = idxIter.next(); indexes.add(index.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } if (dialect.hasAlterTable()) { Iterator<ForeignKey> fkIter = table.getForeignKeyIterator(); while (fkIter.hasNext()) { ForeignKey fk = fkIter.next(); if (fk.isPhysicalConstraint()) { constraints.add(fk.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } } } }
From source file:org.jbpm.db.JbpmSchema.java
License:Open Source License
public String[] getCleanSql() { if (cleanSql == null) { // loop over all foreign key constraints List dropForeignKeysSql = new ArrayList(); List createForeignKeysSql = new ArrayList(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { Iterator subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop foreign key constraint sql dropForeignKeysSql.add( fk.sqlDropString(dialect, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); // and collect the create foreign key constraint sql createForeignKeysSql.add(fk.sqlCreateString(dialect, mapping, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); }/*w w w . j a va2s . c o m*/ } } } List deleteSql = new ArrayList(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); deleteSql.add("delete from " + table.getName()); } // glue // - drop foreign key constraints // - delete contents of all tables // - create foreign key constraints // together to form the clean script List cleanSqlList = new ArrayList(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); } return cleanSql; }
From source file:org.jbpm.identity.hibernate.IdentitySchema.java
License:Open Source License
public String[] getCleanSql() { if (cleanSql == null) { // loop over all foreign key constraints List dropForeignKeysSql = new ArrayList(); List createForeignKeysSql = new ArrayList(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { Iterator subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop key constraint dropForeignKeysSql.add( fk.sqlDropString(dialect, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); createForeignKeysSql.add(fk.sqlCreateString(dialect, mapping, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); }/*www . j ava 2 s. c o m*/ } } } List deleteSql = new ArrayList(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); deleteSql.add("delete from " + table.getName()); } List cleanSqlList = new ArrayList(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); } return cleanSql; }
From source file:org.jbpm.test.Db.java
License:Open Source License
public static void clean(ProcessEngine processEngine) { SessionFactory sessionFactory = processEngine.get(SessionFactory.class); // when running this with a remote ejb invocation configuration, there is no // session factory and no cleanup needs to be done if (sessionFactory == null) { return;// ww w . ja va 2 s. c om } String[] cleanSql = cleanSqlCache.get(processEngine); if (cleanSql == null) { Configuration configuration = processEngine.get(Configuration.class); SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory; Dialect dialect = sessionFactoryImplementor.getDialect(); // loop over all foreign key constraints List<String> dropForeignKeysSql = new ArrayList<String>(); List<String> createForeignKeysSql = new ArrayList<String>(); Iterator<Table> iter = configuration.getTableMappings(); //if no session-factory is build, the configuration is not fully initialized. //Hence, the ForeignKey's won't have a referenced table. This is calculated on //second pass. configuration.buildMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { String catalog = table.getCatalog(); String schema = table.getSchema(); Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop foreign key constraint sql dropForeignKeysSql.add(fk.sqlDropString(dialect, catalog, schema)); // MySQLDialect creates an index for each foreign key. // see // http://opensource.atlassian.com/projects/hibernate/browse/HHH-2155 // This index should be dropped or an error will be thrown during // the creation phase if (dialect instanceof MySQLDialect) { dropForeignKeysSql .add("alter table " + table.getName() + " drop key " + fk.getName()); } // and collect the create foreign key constraint sql createForeignKeysSql .add(fk.sqlCreateString(dialect, sessionFactoryImplementor, catalog, schema)); } } } } List<String> deleteSql = new ArrayList<String>(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { deleteSql.add("delete from " + table.getName()); } } // glue // - drop foreign key constraints // - delete contents of all tables // - create foreign key constraints // together to form the clean script List<String> cleanSqlList = new ArrayList<String>(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); cleanSqlCache.put(processEngine, cleanSql); } Session session = sessionFactory.openSession(); try { for (String query : cleanSql) { // log.trace(query); session.createSQLQuery(query).executeUpdate(); } } finally { session.close(); } }