-->
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.  [ 7 posts ] 
Author Message
 Post subject: Querying table and column names from persistent objects
PostPosted: Mon Nov 21, 2005 1:51 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Is there an API in Hibernate that will return the database table name given the a Hibernate persistent object? Is there also an API that will return the column name given a peristent object's attribute name?

Hibernate version: 3.0

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 1:55 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Yes. Configuration.getMappings() will give u an iterator for all the mappings that were passed to Configuration. The Mappings class has methods to get all kinds of metadata.
Refer to the Javadocs for net.sf.cfg package.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:16 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Is it possible to get a Configuration instance using a SessionFactory object?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:27 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Well, you could do a
SessionFactory.getClassMetaData(Class persistetClass)
to get the class-metadata and from which you can get the rest of the information


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 4:37 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
For some reason the getCollectionMapping(String) operation is returning null for a class name that I know exists in the Hibernate mappings and that I am actively using in my Java program. What would cause this operation to return null instead of the Collection?

String className = MyTestObject.getClass().getName();
// Given Configuration configuration has already be initialized.
Collection collectoin = configuration.getCollectionMapping(className);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 5:16 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
OK. I found a workaround. Given a persistent "object":

String dbTableName = null;
if (null != object) {
String className = object.getClass().getName();
PersistentClass pClass = configuration.getClassMapping(className);
if (null != pClass) {
dbTableName = pClass.getTable().getName();
}
}

My next problem is determining how to query actual column name in the database given that the persistent Java object's column name is known.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 11:23 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
funkhouser wrote:
For some reason the getCollectionMapping(String) operation is returning null for a class name that I know exists in the Hibernate mappings and that I am actively using in my Java program. What would cause this operation to return null instead of the Collection?

String className = MyTestObject.getClass().getName();
// Given Configuration configuration has already be initialized.
Collection collectoin = configuration.getCollectionMapping(className);


You need to enter the fully qualified name of the class (Package + class). It has to match the same class that was passed in during the configuration. I suspect your package information did not get passed to getCollectionMapping thus resulting in null values.


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