List of usage examples for org.hibernate.mapping Table isPhysicalTable
public boolean isPhysicalTable()
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;/* www.j a v a2 s . co m*/ } 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(); } }
From source file:org.jbpm.test.Db.java
License:Open Source License
public static String verifyClean(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 null; }/*from ww w. jav a2s .c o m*/ String[] tableNames = tableNamesCache.get(processEngine); if (tableNames == null) { Configuration configuration = processEngine.get(Configuration.class); // loop over all foreign key constraints List<String> tableNamesList = new ArrayList<String>(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { tableNamesList.add(table.getName()); } } tableNames = tableNamesList.toArray(new String[tableNamesList.size()]); tableNamesCache.put(processEngine, tableNames); } String recordsLeftMsg = ""; Session session = sessionFactory.openSession(); try { for (String tableName : tableNames) { String countSql = "select count(*) as RECORD_COUNT_ from " + tableName; SQLQuery sqlQuery = session.createSQLQuery(countSql); sqlQuery.addScalar("RECORD_COUNT_", Hibernate.LONG); Long recordCount = (Long) sqlQuery.uniqueResult(); if (recordCount > 0L) { recordsLeftMsg += tableName + ":" + recordCount + ", "; } } } finally { session.close(); } if (recordsLeftMsg.length() > 0) { clean(processEngine); } return recordsLeftMsg; }
From source file:org.kuali.mobility.database.service.DatabaseServiceImpl.java
License:Open Source License
private String execute(String dialectStr, String delimiter, boolean overrideAlterTable) { PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo(); Map<String, Object> jpaPropertyMap = entityManagerFactory.getJpaPropertyMap(); jpaPropertyMap.put("hibernate.dialect", dialectStr); Configuration configuration = new Ejb3Configuration().configure(persistenceUnitInfo, jpaPropertyMap) .getHibernateConfiguration(); // KMEDatabaseConfiguration c = (KMEDatabaseConfiguration) configuration; if (overrideAlterTable) { 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()) { subIter.remove(); }//from ww w. j a va2 s .c om } } } } Properties configurationProperties = configuration.getProperties(); Dialect dialect = Dialect.getDialect(configurationProperties); // if (dialect instanceof KMEDialect) { // KMEDialect d = (KMEDialect) dialect; // d.setOverrideAlterTable(overrideAlterTable); // } Properties props = new Properties(); props.putAll(dialect.getDefaultProperties()); props.putAll(configurationProperties); String[] dropSQL = configuration.generateDropSchemaScript(dialect); String[] createSQL = configuration.generateSchemaCreationScript(dialect); Formatter formatter = (PropertiesHelper.getBoolean(Environment.FORMAT_SQL, props) ? FormatStyle.DDL : FormatStyle.NONE).getFormatter(); boolean format = true; formatter = (format ? FormatStyle.DDL : FormatStyle.NONE).getFormatter(); // String delimiter = ";"; StringBuffer output = new StringBuffer(); for (String s : dropSQL) { output.append(formatMe(s, formatter, delimiter)); output.append("\n"); } for (String s : createSQL) { output.append(formatMe(s, formatter, delimiter)); output.append("\n"); } SchemaExport schema = new SchemaExport(configuration); // schema.setFormat(true); // schema.setDelimiter(";"); // schema.setOutputFile("/tmp/schema.sql"); // schema.create(false, false); //org.hibernate.dialect.Oracle10gDialect //org.hibernate.dialect.MySQL5Dialect return output.toString(); }
From source file:org.n52.sos.ds.datasource.AbstractH2Datasource.java
License:Open Source License
@Override public void clear(Properties properties) { Map<String, Object> settings = parseDatasourceProperties(properties); CustomConfiguration config = getConfig(settings); Iterator<Table> tables = config.getTableMappings(); Connection conn = null;/*from w ww . j a v a 2 s .c o m*/ Statement stmt = null; try { conn = openConnection(settings); stmt = conn.createStatement(); stmt.execute("set referential_integrity false"); while (tables.hasNext()) { Table table = tables.next(); if (table.isPhysicalTable()) { stmt.execute("truncate table " + table.getName()); } } stmt.execute("set referential_integrity true"); GeoDB.InitGeoDB(conn); } catch (SQLException ex) { throw new ConfigurationException(ex); } finally { close(stmt); close(conn); } }
From source file:org.n52.sos.ds.datasource.AbstractHibernateDatasource.java
License:Open Source License
@Override public boolean checkIfSchemaExists(Map<String, Object> settings) { Connection conn = null;/*from www . j a v a 2s.com*/ try { /* check if any of the needed tables is existing */ conn = openConnection(settings); DatabaseMetadata metadata = getDatabaseMetadata(conn, getConfig(settings)); Iterator<Table> iter = getConfig(settings).getTableMappings(); String catalog = checkCatalog(conn); String schema = checkSchema((String) settings.get(SCHEMA_KEY), catalog, conn); while (iter.hasNext()) { Table table = iter.next(); if (table.isPhysicalTable() && metadata.isTable(table.getQuotedName()) && metadata.getTableMetadata(table.getName(), schema, catalog, table.isQuoted()) != null) { return true; } } return false; } catch (SQLException ex) { throw new ConfigurationException(ex); } finally { close(conn); } }
From source file:org.n52.sos.ds.datasource.AbstractMySQLDatasource.java
License:Open Source License
@Override public void clear(Properties properties) { Map<String, Object> settings = parseDatasourceProperties(properties); CustomConfiguration config = getConfig(settings); Iterator<Table> tables = config.getTableMappings(); List<String> names = new LinkedList<String>(); while (tables.hasNext()) { Table table = tables.next(); if (table.isPhysicalTable()) { names.add(table.getName());//from w ww. ja v a2 s .c o m } } if (!names.isEmpty()) { Connection conn = null; Statement stmt = null; try { conn = openConnection(settings); stmt = conn.createStatement(); stmt.execute(String.format("truncate %s restart identity cascade", Joiner.on(", ").join(names))); } catch (SQLException ex) { throw new ConfigurationException(ex); } finally { close(stmt); close(conn); } } }
From source file:org.n52.sos.ds.datasource.AbstractOracleDatasource.java
License:Open Source License
@Override public void clear(Properties properties) { Map<String, Object> settings = parseDatasourceProperties(properties); CustomConfiguration config = getConfig(settings); Connection conn = null;//from w w w . j a v a 2s.c o m Statement stmt = null; try { conn = openConnection(settings); stmt = conn.createStatement(); Iterator<Table> tables = config.getTableMappings(); List<String> names = new ArrayList<String>(); while (tables.hasNext()) { Table table = tables.next(); if (table.isPhysicalTable()) { names.add(table.getName()); } } while (names.size() > 0) { int clearedThisPass = 0; for (int i = names.size() - 1; i >= 0; i--) { try { stmt.execute("DELETE FROM " + names.get(i)); names.remove(i); clearedThisPass++; } catch (SQLException ex) { // ignore } } if (clearedThisPass == 0) { throw new RuntimeException("Cannot clear!"); } } conn.commit(); } catch (SQLException e) { throw new RuntimeException("Cannot clear!", e); } finally { close(stmt); close(conn); } }
From source file:org.n52.sos.ds.datasource.AbstractSqlServerDatasource.java
License:Open Source License
@Override public void clear(Properties properties) { Map<String, Object> settings = parseDatasourceProperties(properties); CustomConfiguration config = getConfig(settings); Iterator<Table> tables = config.getTableMappings(); List<String> names = new LinkedList<String>(); while (tables.hasNext()) { Table table = tables.next(); if (table.isPhysicalTable()) { names.add(table.getName());//from w w w . j a v a2s . c om } } if (!names.isEmpty()) { Connection conn = null; Statement stmt = null; try { conn = openConnection(settings); stmt = conn.createStatement(); StringBuffer statement = new StringBuffer(); // alter table MyOtherTable nocheck constraint all for (String table : names) { statement = statement.append("ALTER TABLE \"").append(table) .append("\" NOCHECK CONSTRAINT ALL;"); } // delete from MyTable for (String table : names) { statement = statement.append("DELETE from \"").append(table).append("\"; DBCC CHECKIDENT(\"") .append(table).append("\", RESEED, 0);"); } // alter table MyOtherTable check constraint all for (String table : names) { statement = statement.append("ALTER TABLE \"").append(table).append("\" CHECK CONSTRAINT ALL;"); } statement = statement.append("DBCC SHRINKDATABASE (").append(settings.get(DATABASE_KEY).toString()) .append(");"); stmt.execute(statement.toString()); } catch (SQLException ex) { throw new ConfigurationException(ex); } finally { close(stmt); close(conn); } } }
From source file:org.n52.sos.ds.datasource.CustomConfiguration.java
License:Open Source License
protected boolean checkTable(Table table, DatabaseMetadata m) { return table.isPhysicalTable() && m.isTable(table.getQuotedName()); }
From source file:org.n52.sos.ds.datasource.PostgresDatasource.java
License:Open Source License
@Override public void clear(Properties properties) { Map<String, Object> settings = parseDatasourceProperties(properties); CustomConfiguration config = getConfig(settings); Iterator<Table> tables = config.getTableMappings(); List<String> names = new LinkedList<String>(); while (tables.hasNext()) { Table table = tables.next(); if (table.isPhysicalTable()) { names.add(table.getName());/*from w w w .j a v a 2s . c om*/ } } if (!names.isEmpty()) { Connection conn = null; Statement stmt = null; try { conn = openConnection(settings); stmt = conn.createStatement(); stmt.execute(String.format("truncate %s restart identity cascade", StringHelper.join(", ", names))); } catch (SQLException ex) { throw new ConfigurationException(ex); } finally { close(stmt); close(conn); } } }