I'm new to this topic, too. I'm using xml and overwrite the name of a table, which I mapped with XML programatically. It works but I'm not satisfied, yet.
I think it's possible to overwrite the whole mapping programatically.
Example for changing the name of a table from TEST to TEST2:
Code:
SessionFactory sessionFactory;
Configuration configuration;
try{
configuration = new Configuration()
.configure("mysql.cfg.xml");
Iterator iterator = configuration.getTableMappings();
while (iterator.hasNext()) {
Table table = (Table) iterator.next();
if (table.getName().equals("TEST")) {
table.setName("TEST2");
}
System.out.println(table.getCatalog());
System.out.println(table.getName());
}
sessionFactory = configuration
.buildSessionFactory();
}
catch(Throwable ex){
throw new ExceptionInInitializerError(ex);
}
It's important to create an new SessionFactory. For changing mappings in runtime you always need to create an new SessionFactory.
From Table you can get columns, etc. so you should be able to change all your mappings in runtime.