OK, the way you get Hibernate going is something like:
Code:
// This could be a static
protected SessionFactory sessions;
// snip....
Configuration cfg = new Configuration();
// Load the cfg properties ...
if (propFile != null) {
Properties props = new Properties();
props.load( new FileInputStream(propFile) );
cfg.setProperties(props);
}
// From a list of file names, add .hbm.xml files
if ( fileNames != null ) {
Iterator it = fileNames.iterator();
while ( it.hasNext() ) {
String filename = (String) it.next();
cfg.addFile(filename);
}
}
sessions = cfg.buildSessionFactory();
This is where the reading of the XML files occurs. With your dynamic tables, you would have to go through this each time you add a new table (do you create them on the fly?) or want to change a mapping of a class to a different table.
Past that point, to load objects, you would be doing:
Code:
Session aSession = null;
try {
aSession = sessions.openSession();
// work with the session - load objects etc
aSession.flush();
aSession.connection().commit();
} catch (Exception e) {
e.printStackTrace();
if (aSession != null)
aSession.connection().rollback();
} finally {
if (aSession != null) {
aSession.close();
}
}
or something with Transactions.
This does not load the XML files again. You should not be creating the SessionFactory each time you want to do Hibernate work in your app.
The time it takes to create the SessionFactory varies depending on the complexity of the model you are dealing with: number of classes and relationships being mapped etc. Hibernate 2.1 is a lot faster at this than 2.0 - the change to cglib2 is the cause. When I start JBoss now under 2.1, the Hibernate initialization of my 25 classes and 8 collections is 1 sec.
Cheers,
Sherman