org.hibernate.tool.hbm2ddl.SchemaUpdate Java Examples

The following examples show how to use org.hibernate.tool.hbm2ddl.SchemaUpdate. 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: MigrationTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testSimpleColumnAddition() {
	String resource1 = "org/hibernate/test/schemaupdate/1_Version.hbm.xml";
	String resource2 = "org/hibernate/test/schemaupdate/2_Version.hbm.xml";

	Configuration v1cfg = new Configuration();
	v1cfg.addResource( resource1 );
	new SchemaExport( v1cfg ).execute( false, true, true, false );

	SchemaUpdate v1schemaUpdate = new SchemaUpdate( v1cfg );
	v1schemaUpdate.execute( true, true );

	assertEquals( 0, v1schemaUpdate.getExceptions().size() );

	Configuration v2cfg = new Configuration();
	v2cfg.addResource( resource2 );

	SchemaUpdate v2schemaUpdate = new SchemaUpdate( v2cfg );
	v2schemaUpdate.execute( true, true );
	assertEquals( 0, v2schemaUpdate.getExceptions().size() );

}
 
Example #2
Source File: TestSchemaTools.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSchemaTools() throws Exception{
	// database schema have been created thanks to the setUp method
	// we have 2 schemas SA et SB, SB must be set as the default schema
	// used by hibernate hibernate.default_schema SB
	SchemaExport se = new SchemaExport(getCfg());
	se.create(true,true);
	
	// here we modify the generated table in order to test SchemaUpdate
	Session session = openSession();
	Connection conn = session.connection();
	Statement stat = conn.createStatement();
	stat.execute("ALTER TABLE \"SB\".\"Team\" DROP COLUMN name ");
	
	// update schema
	SchemaUpdate su = new SchemaUpdate(getCfg());
	su.execute(true,true);
	
	// we can run schema validation. Note that in the setUp method a *wrong* table
	// has been created with different column names
	// if schema validator chooses the bad db schema, then the testcase will fail (exception)
	SchemaValidator sv = new SchemaValidator(getCfg());
	sv.validate();
	
	// it's time to clean our database
	se.drop(true,true);
	
	// then the schemas and false table.

	stat.execute("DROP TABLE \"SA\".\"Team\" ");
	stat.execute(" DROP SCHEMA sa ");
	stat.execute("DROP SCHEMA sb ");
	stat.close();
	session.close();
}
 
Example #3
Source File: TestSchemaTools.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSchemaToolsNonQuote() throws Exception{
	// database schema have been created thanks to the setUp method
	// we have 2 schemas SA et SB, SB must be set as the default schema
	// used by hibernate hibernate.default_schema SB
	SchemaExport se = new SchemaExport(getCfg());
	se.create(true,true);
	
	// here we modify the generated table in order to test SchemaUpdate
	Session session = openSession();
	Connection conn = session.connection();
	Statement stat = conn.createStatement();
	stat.execute("ALTER TABLE \"SB\".\"TEAM\" DROP COLUMN xname ");
	
	// update schema
	SchemaUpdate su = new SchemaUpdate(getCfg());
	su.execute(true,true);
	
	// we can run schema validation. Note that in the setUp method a *wrong* table
	// has been created with different column names
	// if schema validator chooses the bad db schema, then the testcase will fail (exception)
	SchemaValidator sv = new SchemaValidator(getCfg());
	sv.validate();
	
	// it's time to clean our database
	se.drop(true,true);
	
	// then the schemas and false table.

	stat.execute("DROP TABLE \"SA\".\"Team\" ");
	stat.execute(" DROP SCHEMA sa ");
	stat.execute("DROP SCHEMA sb ");
	stat.close();
	session.close();
}
 
Example #4
Source File: HibernateEngine.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void configureSession(Properties props, List<Class> classes) {
    Configuration cfg = new Configuration();
    cfg.setProperties(props);

    if (classes != null) {
        for (Class className : classes) {
            cfg.addClass(className);
        }
    }

    _factory = cfg.buildSessionFactory();         // get a session context        

    // check tables exist and are of a matching format to the persisted objects
    new SchemaUpdate(cfg).execute(false, true);
}
 
Example #5
Source File: HibernateEngine.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** initialises hibernate and the required tables */
private void initialise(Set<Class> classes, Properties props) throws HibernateException {
    try {
        Configuration _cfg = new Configuration();

        // if props supplied, use them instead of hibernate.properties
        if (props != null) {
            _cfg.setProperties(props);
        }

        // add each persisted class to config
        for (Class persistedClass : classes) {
            _cfg.addClass(persistedClass);
        }

        // get a session context
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(_cfg.getProperties()).build();
        _factory = _cfg.buildSessionFactory(serviceRegistry);

        // check tables exist and are of a matching format to the persisted objects
        new SchemaUpdate(_cfg).execute(false, true);

    }
    catch (MappingException me) {
        _log.error("Could not initialise database connection.", me);
    }
}
 
Example #6
Source File: DatabaseSetup.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Generates alter database DDL and EXECUTES it.
 */
private static void updateDatabaseDDL() {
    boolean printToOut = true; // write to System.out
    boolean updateDatabase = false;
    try {
        new SchemaUpdate(cf).execute(printToOut, updateDatabase);
    } catch (Exception e) {
        log.error("DDL export to file failed: Reason: ", e);
    }
}
 
Example #7
Source File: DatabaseSetup.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Generates alter database DDL and EXECUTES it.
 */
private static void updateDatabaseDDL() {
    boolean printToOut = true; // write to System.out
    boolean updateDatabase = false;
    try {
        new SchemaUpdate(cf).execute(printToOut, updateDatabase);
    } catch (Exception e) {
        log.error("DDL export to file failed: Reason: ", e);
    }
}
 
Example #8
Source File: SchemaExport.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
/** Updates the current DB-Schema with the schema defined by the hbm.xml files.
 * 
 * @param script Print the DDL to the console.
 * @param export
 */
public void updateSchema(Configuration cfg, boolean script, boolean export) {
  try {
    SchemaUpdate exp = new SchemaUpdate(cfg);
    exp.execute(script, export);
  } catch (HibernateException ex) {
    log.fatal("Cant't update database schema: " + ex.getMessage(), ex);
    return;
  }
}
 
Example #9
Source File: MamuteDatabaseConfiguration.java    From mamute with Apache License 2.0 4 votes vote down vote up
public SchemaUpdate getSchemaUpdate() {
	return new SchemaUpdate(cfg);
}