-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to get table name by code?
PostPosted: Thu Aug 31, 2006 9:53 am 
Newbie

Joined: Thu Dec 23, 2004 3:53 am
Posts: 19
Hibernate version:
3.0.5

Can I get the table name of and entity I am working with.
The ClassMetaData class does not give that info although when evaluting the ClassMetaData value I can see it there in field called QulifiedTableNames?

Oded


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:19 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
hello

try it like that:
Code:

LocalSessionFactoryBean lsfb = getLocalSessionFactoryBean();
      ClassMetadata meta = getSessionFactoryBean().getClassMetadata(entity.getClass());

      String[] propertyNames = meta.getPropertyNames();
      Object[] values = meta.getPropertyValues(entity, EntityMode.POJO);
      boolean[] nullAllowed = meta.getPropertyNullability();
      Type[] types = meta.getPropertyTypes();

      Configuration cfg = lsfb.getConfiguration();
      PersistentClass classMapping = cfg.getClassMapping(entity.getClass().getName());

      Table table = classMapping.getTable();

//more


regards angela


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:33 am 
Newbie

Joined: Thu Dec 23, 2004 3:53 am
Posts: 19
I have more than one LocalSessionFactoryBean. How to get one?

Oded


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 11:01 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
shushu wrote:
I have more than one LocalSessionFactoryBean. How to get one?

Oded


i have more than one as well. I'm using Spring so i can define which one i would like to receive:

Code:
   LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ContextUtils
            .getApplicationContext().getBean("&defaultSessionFactory");
      return lsfb;


Angela


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 11:27 am 
Newbie

Joined: Thu Dec 23, 2004 3:53 am
Posts: 19
Sorry, but what is ContextUtils???


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 1:12 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
shushu wrote:
Sorry, but what is ContextUtils???


This is a class i wrote, it's using the Spring context to get the SessionFactoryBean.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 1:16 am 
Newbie

Joined: Thu Dec 23, 2004 3:53 am
Posts: 19
Sorry but this is not very helpful, can u publish the class ContextUtils code?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 1:58 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
shushu wrote:
Sorry but this is not very helpful, can u publish the class ContextUtils code?


So you are using the Spring Framework as well?! Not only hibernate? This is a class we customized for our needs in the application, it won't help you.

Code:
public class ContextUtils {

   private static ClassPathXmlApplicationContext ctx = null;

   /**
    * Gibt den aktuellen Spring ApplicationContext zurueck. Wird bei bedarf erstellt (Singelton).
    *
    * @return ApplicationContext
    */
   public static ApplicationContext getApplicationContext() {

      if (ctx == null) {
         String[] configLocations = null;
         // reading config from properties file springContext.properties
         try {
            Properties props = new Properties();
            InputStream is = ContextUtils.class.getClassLoader().getResourceAsStream("springContext.properties");
            props.load(is);

            String contextFiles = props.getProperty("contextFiles");
            StringTokenizer tok = new StringTokenizer(contextFiles, ",");
            configLocations = new String[tok.countTokens()];
            for (int idx = 0; tok.hasMoreTokens(); idx++) {
               configLocations[idx] = tok.nextToken().trim();
            }
         }
         catch (IOException e) {
            e.printStackTrace();
         }
         ctx = new ClassPathXmlApplicationContext(configLocations);
      }
      return ctx;
   }

}



Did you search the forum for your problem?? You should be able to get the session Factory throught your hibernate configuration. Something like:

Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

ClassMetadata meta = sessionFactory.getClassMetadata(StakeHolder.class) ;


angela


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 03, 2006 11:57 am 
Newbie

Joined: Thu Dec 23, 2004 3:53 am
Posts: 19
Currently I'm not caching ny configuration objects. The session factory is fetched from the application context of the registry files loaded, previously you showed this code

Code:
[quote]LocalSessionFactoryBean lsfb = (LocalSessionFactoryBean) ContextUtils       .getApplicationContext().getBean("&defaultSessionFactory");[/quote]


How you cast a sessionFactory (that is Hibernate object) to LocalSessionFactoryBean (that is Spring object) or this (&defaultSessionFactory) is a special one?

Oded


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