-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to resolve persistent class from table name
PostPosted: Tue Jul 31, 2007 5:29 am 
Newbie

Joined: Tue Jul 31, 2007 5:14 am
Posts: 3
Hello,

Given a table name, is there any way to look up what persistent class it resolves to? I have a text table name and primary key and want to get hold of the corresponding hibernate object. I suppose I could parse the mapping files myself and have some kind of factory but I am hoping as these are already parsed by hibernate that there might be some mechanism through the API.

I've tried searching the forums but all the keywords I could think of were a bit too generic to get any useful results.

Thanks very much.

Hibernate version: 3.2.0-ga


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 7:52 am 
Newbie

Joined: Tue Jul 31, 2007 5:16 am
Posts: 9
Perhaps you find somthing in
Code:
HibernateUtil().getSessionFactory().getAllClassMetadata()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 7:57 am 
Newbie

Joined: Tue Jul 31, 2007 5:14 am
Posts: 3
mrfox wrote:
Perhaps you find somthing in
Code:
HibernateUtil().getSessionFactory().getAllClassMetadata()


Hi thanks for the reply. I tried everything I could think of on that but couldn't find anything to match against the table name - just the entity name.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 7:58 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you have a reference to the Configuration object you can find the class and table names there:
Code:
Configuration config = new Configuration().addClass(.....);

        Iterator<RootClass> iterator = config.getClassMappings();
        while (iterator.hasNext()) {
           RootClass rootClass = iterator.next();
           System.out.println("class: " + rootClass.getClassName() + " maps to table: " + rootClass.getTable().getName());
        }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 9:00 am 
Newbie

Joined: Tue Jul 31, 2007 5:14 am
Posts: 3
thatmikewilliams wrote:
If you have a reference to the Configuration object you can find the class and table names there:
Code:
Configuration config = new Configuration().addClass(.....);

        Iterator<RootClass> iterator = config.getClassMappings();
        while (iterator.hasNext()) {
           RootClass rootClass = iterator.next();
           System.out.println("class: " + rootClass.getClassName() + " maps to table: " + rootClass.getTable().getName());
        }


Thats perfect. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 9:05 am 
Newbie

Joined: Sun Apr 09, 2006 8:38 am
Posts: 14
you can also try:
1. map the class hierarchy as table-per-hierarchy scheme.
2. set the discriminator value to be the class name
2. perform a select on the discriminator column of the table
e.g. "select distinct DISC_VAL from <myTableNAme>
3. in java, do a Class.forName() on each result.


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