Thanks for the responses.
Pete, I think I understand the gist of what you are saying; however, I'm not currently using any DataSource, Connection, Statement, and PreparedStatement classes... so I'm not sure how to relate it to my work. Below is the code I am using.
First, my method to get the databases set up (configs, sessionFactories, etc.):
Code:
public static void initializeDatabases()
{
try {
// Create Configurations from configuration files.
Configuration mysqlConfig = new Configuration().configure("/hibernate_mysql.cfg.xml");
Configuration derbyConfig = new Configuration().configure("/hibernate_derby.cfg.xml");
// Check to see if the Derby database is already present.
File dbLoc = new File("db/msjdb");
if (!dbLoc.exists()) {
// Change the connection URL to create a new database.
String prop = "hibernate.connection.url";
String url = derbyConfig.getProperty(prop);
derbyConfig.setProperty(prop, url + ";create=true");
// Export the schema to Derby.
SchemaExport se = new SchemaExport(derbyConfig);
se.create(false, true);
// Change the connection URL back to the original.
derbyConfig.setProperty(prop, url);
}
// Create SessionFactories from configurations.
ourMysqlSessionFactory = mysqlConfig.buildSessionFactory();
ourDerbySessionFactory = derbyConfig.buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
And here is the method I use to transfer the result of an HQL query from MySQL to Derby:
Code:
public static void transferHqlResults(String hqlQuery)
{
// Open the MySQL session.
Session mysqlSession = getMysqlSessionFactory().openSession();
// Query the MySQL database for pojos.
Query q = mysqlSession.createQuery(hqlQuery);
List<MsjHbPojo> result = q.list();
// Recursively build a list of objects that will be persisted.
List<Object> reqdObjects = new Vector<Object>();
for (int i = 0; i < result.size(); i++)
{
importWithPrereqs(result.get(i), reqdObjects, mysqlSession);
}
// Close the MySQL session.
mysqlSession.close();
// Open the Derby session.
Session derbySession = getDerbySessionFactory().openSession();
// Begin the Derby transaction.
Transaction t = derbySession.beginTransaction();
// Save all the pojos that have been loaded.
for (int p = 0; p < reqdObjects.size(); p++)
{
if (reqdObjects.get(p) instanceof MsjHbPojo)
{
MsjHbPojo pojo = (MsjHbPojo)reqdObjects.get(p);
Class c = ClassHelper.getNormalizedClass(pojo.getClass());
MsjHbPojo loadPojo = (MsjHbPojo)derbySession.get(c, pojo.getId());
if (loadPojo==null)
{
derbySession.save(reqdObjects.get(p));
}
}
}
// Commit to Derby.
t.commit(); // ***THE SQL THAT I WANT TO CAPTURE GETS PRINTED HERE FROM LOG4J***
// Close the Derby session.
derbySession.close();
}
I'm pretty much a newbie when it comes to Hibernate. This is my first project... and it took me forever to get this working the way I wanted it. Do you have any suggestions that would be straightforward with my current code? Thanks...
-RN