-->
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.  [ 5 posts ] 
Author Message
 Post subject: Modify Hibernate mapping attribute runtime
PostPosted: Thu Dec 29, 2011 10:54 am 
Newbie

Joined: Thu Nov 03, 2011 1:43 pm
Posts: 5
I have the following Hibernate mapping (hbm.xml) file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="org.mycompany.project.SampleItem"
       table="MY_TABLE_VIEW" mutable="false">
      <composite-id>
         <key-property name="version" type="java.lang.String">
            <column name="VERSION"/>
         </key-property>
         <key-property name="itemId" type="java.lang.String">
            <column name="ITEM_ID"/>
         </key-property>
      </composite-id>
      <property name="sampleItemValue" type="java.lang.String">
         <column name="SAMPLE_ITEM_VALUE"/>
      </property>
   </class>
</hibernate-mapping>

I need run time (programmatically) to change the ‘table’ attribute: table="MY_OTHER_TABLE_VIEW". How can I do it (if at all)?


Top
 Profile  
 
 Post subject: Re: Modify Hibernate mapping attribute runtime
PostPosted: Thu Dec 29, 2011 11:10 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
How often do you have to "remap" the table? Only once when started/initialized first? Then you can provide your own NamingStrategy that reads the correct tablename from somewhere else and returns it instead of the name inside the mapping.


Top
 Profile  
 
 Post subject: Re: Modify Hibernate mapping attribute runtime
PostPosted: Thu Dec 29, 2011 6:14 pm 
Newbie

Joined: Thu Nov 03, 2011 1:43 pm
Posts: 5
Thanks for your response. Yes, I need to "remap" on start-up only. I am not familiar with NamingStrategy API, thus need to look it up.


Top
 Profile  
 
 Post subject: Re: Modify Hibernate mapping attribute runtime
PostPosted: Fri Dec 30, 2011 2:46 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
A very simplified version:

Code:
package a.b.c.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cfg.DefaultNamingStrategy;

public class SimpleNamingStrategy extends DefaultNamingStrategy {

   private static final long serialVersionUID = 635776071048516410L;
   
   private String viewName;

   public SimpleNamingStrategy() {
      viewName= System.getProperty("view_xy");
   }

   @Override
   public String classToTableName(String className) {
      String rc = super.classToTableName(className);
      if (className.endsWith("view_xy")) {
         if (viewName!= null) {
            rc = viewName;
         }
      }
      return rc;
   }

}



Usage in start-up:

Quote:
Configuration cfg = new Configuration().setNamingStrategy(new SimpleNamingStrategy());


Top
 Profile  
 
 Post subject: Re: Modify Hibernate mapping attribute runtime
PostPosted: Tue Jan 03, 2012 12:26 pm 
Newbie

Joined: Thu Nov 03, 2011 1:43 pm
Posts: 5
Thanks for the help, solution worked. The only small caveat: Hibernate mapping (hbm.xml) file must not have “table” attribute any longer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.