Hi !
I am working on a project in which I have to create database table at run time.
how can I map hbm files at run time.
I tried with below given code,
but with following code I have to restart the tomcat each time I created a new table in database.
Please somebody guide me that how can I map the hbm file without restarting the server
Thanks
Code:
public class DynamicTableUtil {
private static SessionFactory sessionFactory;
static {
try{
Configuration cfg = new Configuration();
cfg.addResource("hbms/hbmtable.hbm.xml");
DynamicTableUtil dtu = new DynamicTableUtil();
List l = dtu.getDatafromTable(null);
for(int i=0;i<l.size();i++){
cfg.addResource(((Map)l.get(i)).get("tables").toString());
}
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
sessionFactory = cfg.buildSessionFactory();
}catch(Throwable ex){
System.err.println("Initial Session Factory Creation Failed "+ex);
throw new ExceptionInInitializerError(ex);
}
}
public List getDatafromTable(String tablename){
List l = null;
Session pojosession = HibernateUtil.getSessionFactory().openSession();
Session dynamicsession = pojosession.getSession(EntityMode.MAP);
l=dynamicsession.createQuery("select new map(tables as tables) from hbmtable").list();
dynamicsession.flush();
pojosession.flush();
dynamicsession.close();
pojosession.close();
return l;
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}