-->
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.  [ 3 posts ] 
Author Message
 Post subject: Programmatic Configuration mixed with xml. Possible?
PostPosted: Wed Aug 08, 2007 8:28 am 
Beginner
Beginner

Joined: Sat Feb 10, 2007 12:18 pm
Posts: 23
Hi

I am using Hibernate in a Eclipse RCP application where driver, dialect, subprotocol are set using an extension point and the Ip, Databasename, User and Password are set using the application preferences. Due to that I set the Configuration programmatically. All works fine except that my mapping files are not added to the configuration yet.

I assumed that the xml is loaded first and the programmatic settings override it where necessary. Since this seemed to be wrong I would like to know how how I can add my mappings without having them hard-coded?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 9:44 am 
Newbie

Joined: Sun May 06, 2007 5:57 am
Posts: 10
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 10:07 am 
Beginner
Beginner

Joined: Sat Feb 10, 2007 12:18 pm
Posts: 23
Thanks, it works.

I forgot to add .configure("hibernate.cfg.xml") to my configuration. Now all is loaded as desired and special settings are overriden programmatically.


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

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.