org.hibernate.tool.hbm2ddl.SchemaValidator Java Examples

The following examples show how to use org.hibernate.tool.hbm2ddl.SchemaValidator. 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: DbInitializer.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Schema validation
 */
// TODO
private void validateSchema() {
    try {
        SessionFactory factory = this.localSessionFactory.unwrap(SessionFactory.class);
        StandardServiceRegistry registry = factory.getSessionFactoryOptions().getServiceRegistry();
        MetadataSources sources = new MetadataSources(registry);
        sources.addPackage("org.unitedinternet.cosmo.model.hibernate");
        Metadata metadata = sources.buildMetadata(registry);
        new SchemaValidator().validate(metadata);
        LOG.info("Schema validation passed");
    } catch (HibernateException e) {
        LOG.error("error validating schema", e);
        throw e;
    }
}
 
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: TestSchemaTools.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testFailingQuoteValidation() 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);
	
	try {
		SchemaValidator sv = new SchemaValidator(getCfg());
		sv.validate();
		fail("should fail since we mutated the current schema.");
	} catch(HibernateException he) {
		
	}
	
	// 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 #5
Source File: TestSchemaTools.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testFailingNonQuoteValidation() 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);
	
	try {
		SchemaValidator sv = new SchemaValidator(getCfg());
		sv.validate();
		fail("should fail since we mutated the current schema.");
	} catch(HibernateException he) {
		
	}
	
	// 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();
}