Another problem that I found during my projects is the database creation.
A good design practice is to let hibernate create its own database by itself. I searched for a long time around the net and I found a simple and really useful guide here.
You can find it there however I will put a copy of that code here!
package com.cascadetg.ch11;
/** Various Hibernate-related imports */
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import java.util.Properties;
public class SchemaGeneratorExample
{
// System constants for the current platform directory
token
static String fileSep =
System.getProperty("file.separator");
/** We use this session factory to create our sessions */
public static SessionFactory sessionFactory;
static String[] db_dialects =
{"Mysql",
"com.mysql.jdbc.Driver", //
"DB2", //
"net.sf.hibernate.dialect.DB2Dialect", //
"DB2400", //
"net.sf.hibernate.dialect.DB2400Dialect", //
"Firebird", //
"net.sf.hibernate.dialect.FirebirdDialect", //
"FrontBase", //
"net.sf.hibernate.dialect.FrontBaseDialect", //
"Generic", //
"net.sf.hibernate.dialect.GenericDialect", //
"HypersonicSQL", //
"net.sf.hibernate.dialect.HSQLDialect", //
"Informix", //
"net.sf.hibernate.dialect.InformixDialect", //
"Informix9", //
"net.sf.hibernate.dialect.Informix9Dialect", //
"Ingres", //
"net.sf.hibernate.dialect.IngresDialect", //
"Interbase", //
"net.sf.hibernate.dialect.InterbaseDialect", //
"Mckoi SQL", //
"net.sf.hibernate.dialect.MckoiDialect", //
"Microsoft SQL Server", //
"net.sf.hibernate.dialect.SQLServerDialect", //
"MySQL", //
"net.sf.hibernate.dialect.MySQLDialect", //
"Oracle 9", //
"net.sf.hibernate.dialect.Oracle9Dialect", //
"Oracle", //
"net.sf.hibernate.dialect.OracleDialect", //
"Pointbase", //
"net.sf.hibernate.dialect.PointbaseDialect", //
"PostgreSQL", //
"net.sf.hibernate.dialect.PostgreSQLDialect", //
"Progress", //
"net.sf.hibernate.dialect.ProgressDialect", //
"SAP DB", //
"net.sf.hibernate.dialect.SAPDBDialect", //
"Sybase Anywhere", //
"net.sf.hibernate.dialect.SybaseAnywhereDialect",
"Sybase 11.9.2", //
"net.sf.hibernate.dialect.Sybase11_9_2Dialect", //
"Sybase", //
"net.sf.hibernate.dialect.SybaseDialect",};
public static void main(String[] args)
{
initialization();
}
/**
* Loads the Hibernate configuration information, sets up the
* database and the Hibernate session factory.
*/
public static void initialization()
{
System.out.println("initialization");
try
{
Configuration myConfiguration = new
Configuration();
/**
* Insert here your beans classes for which you want to generate the SQL script
* one for each bean for each table
**/
myConfiguration.addClass(com.cascadetg.ch03.Owner.class);
myConfiguration.addClass(com.cascadetg.ch03.Artifact.class);
Properties myProperties = new Properties();
for (int i = 0; i < db_dialects.length; i = i + 2)
{
String dialect_name = db_dialects[i];
String dialect_class = db_dialects[i + 1];
String dialect_file =
dialect_name.toLowerCase();
dialect_file = dialect_file.replace(' ', '_');
dialect_file += (".sql");
String path = "com" + fileSep + "cascadetg"
+ fileSep + "ch11" + fileSep;
System.out.println("Generating " +
dialect_name);
// Note that this is the only Hibernate property
// set. In particular, there is no JDBC
// connectivity data, nor are we specifying a
// driver!
myProperties.put("hibernate.dialect",
dialect_class);
try
{
// Load the *.hbm.xml files as set in the
// config, and set the dialect.
SchemaExport mySchemaExport = new
SchemaExport(
myConfiguration, myProperties);
mySchemaExport.setDelimiter(";");
// Despite the name, the generated create
// scripts WILL include drop statements at
// the top of the script!
mySchemaExport.setOutputFile(path + "create_"
+ dialect_file);
mySchemaExport.create(false, false) ;
// Generates DROP statements only
mySchemaExport.setOutputFile(path + "drop_"
+ dialect_file);
mySchemaExport.drop(false, false);
System.out.println(dialect_name + " OK.");
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Note that in order to make this simple class run, we need to attach to our Java build path a lot of libraries (specially the one needed for the creation of the scripts). Of course if you don't have the libraries in your console you will see some error lines(something about missing libraries).
I found this code really useful. You have only to modify the path in which you want to save the two output files (create and drop) and that's all!
No comments:
Post a Comment