下面列出了怎么用org.hibernate.tool.hbm2ddl.SchemaUpdate的API类实例代码及写法,或者点击链接到github查看源代码。
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() );
}
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();
}
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();
}
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);
}
/** 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/** 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;
}
}
public SchemaUpdate getSchemaUpdate() {
return new SchemaUpdate(cfg);
}