-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: how to add hibernate mapping file dynamically
PostPosted: Fri Sep 10, 2010 7:17 am 
Newbie

Joined: Wed Aug 25, 2010 4:35 am
Posts: 8
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;
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.