-->
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: One Class/Mapping - Multiple Tables
PostPosted: Wed Oct 25, 2006 11:17 am 
Newbie

Joined: Wed Oct 25, 2006 11:10 am
Posts: 3
Location: Memphis
Here's what I'm trying to do:

I have a class (CW1) and a mapping file(CW1.hbm.xml) with the table mapping like so:

Code:
<class name="CW1" table="Y2K.CW100039">


I want to be able to change the table to "Y2K.CW100654" at runtime because we have several tables with different CW1... names to represent special events we hold and I'm developing an application to dynamically insert registration info.

I can't map the tables traditionally becuase the "CW1XXXXX" name is ever expanding...meaning they are always adding new events.

Is there a way to do this? Examples?

*Add on: I should also mention that I'm using the Spring/Hibernate framework if that makes a difference or not.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 3:09 pm 
Newbie

Joined: Wed Oct 25, 2006 11:10 am
Posts: 3
Location: Memphis
Surely I can't be the first one to attempt this...


Top
 Profile  
 
 Post subject: SOLUTION
PostPosted: Fri Oct 27, 2006 9:04 am 
Newbie

Joined: Wed Oct 25, 2006 11:10 am
Posts: 3
Location: Memphis
Here's the solution:

Code:
/**********************************************
* Initialize Spring/Hibernate SessionFactory *
**********************************************/
Cw1SpringDao dao = SpringRegistry.getCw1Dao();

....

/***********************************************
* Build Hibernate Configuration manually                       *
*                                                                                *
* This is necessary to manipulate xml metadata             *
* at runtime (changing the mapped table)                      *
************************************************/
Configuration cfg = new Configuration()
    .addClass(CW1.class)
    .setProperty("hibernate.dialect","org.hibernate.dialect.DB2400Dialect")
    .setProperty("hibernate.driver_class","com.ibm.as400.access.AS400JDBCDriver")
    .setProperty("hibernate.connection.url","jdbc:as400://System")
    .setProperty("hibernate.connection.username","uname")
    .setProperty("hibernate.connection.password","pass")
    .setProperty("hibernate.show_sql","true");

PersistentClass cw1Mapping = cfg.getClassMapping(CW1.class.getName());
Table table = cw1Mapping.getTable();
log.info("Original table from xml: "+table);
// prints Original table from xml: org.hibernate.mapping.Table(Y2K.CW100654)

...

for(int i = 0; i < num; i++){
    ...
    table.setName("newTableName");
    log.info("New table name: "+table);
    // prints New table name: org.hibernate.mapping.Table (Y2K.CW100732)
    /***********************************************
      * Build SessionFactory from Configuration                   *
      * and pass it to Spring/Hibernate SessionFactory         *
      **********************************************/
      SessionFactory sf = cfg.buildSessionFactory();
      dao.setSessionFactory(sf);
      // save cw1
      dao.saveOrUpdate(cw1);
     
    ...
}


Hope this helps someone with similiar problems...


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.