Hi,
I already searched this forum and google, but couldn't find exactly what I was looking for.
I would like to have an extra NamingStrategy, called TestNamingStrategy which concats all my table names with "_TEST".
While in normal mode, I would like to use the DefaultNamingStrategy; But while running the unit tests I would like to use the TestNamingStrategy.
I have the following HibernateUtils class implemented in a separate utils project outside the project I'm working on and I would like to be able to use this single class (which is also unit tested).
I think this HibernateUtil class was practically taken from the Hibernate Community FAQs or something. I'll leave the comments and Javadocs out.
Code:
public class HibernateUtils {
private static SessionFactory sessionFactory;
static {
HibernateUtils.init();
}
public static void init() {
Configuration config = HibernateUtils.getInitializedConfiguration();
sessionFactory = config.buildSessionFactory();
}
public static Configuration getInitializedConfiguration() {
Configuration config = new Configuration();
config.configure();
return config;
}
public static Session getSession() {
Session hibernateSession = sessionFactory.getCurrentSession();
return hibernateSession;
}
public static void flushSession() {
HibernateUtils.getSession().flush();
}
public static void closeSession() {
HibernateUtils.getSession().close();
}
public static void recreateDatabase() {
Configuration config;
config = HibernateUtils.getInitializedConfiguration();
new SchemaExport(config).setDelimiter(";").setFormat(true).setOutputFile("./target/schema-export.sql").create(
false, true);
}
public static Session beginTransaction() {
Session hibernateSession;
hibernateSession = HibernateUtils.getSession();
hibernateSession.beginTransaction();
return hibernateSession;
}
public static void commitTransaction() {
HibernateUtils.getSession().getTransaction().commit();
}
public static void rollbackTransaction() {
HibernateUtils.getSession().getTransaction().rollback();
}
public static void main(String args[]) {
HibernateUtils.recreateDatabase();
}
}
Problem is, I can only switch NamingStrategy inside the code where I'm creating the Configuration.
Apparently I cannot use hibernate.cfg.xml to specify which NamingStrategy to use (I have separate hibernate.cfg.xml files for the project and for the unit tests).
I cannot dynamically pass a TEST_MODE parameter of some sorts due to the static character of the class, nor can I override specific methods.
Can anyone give me some advice as how to manage this, except hard-coding my Unit Test mapping files to
Code:
... table="FOO_TEST">...
?
Thanks in advance, and kind regards.
Don Stevo