-->
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: Accessing hibernate's mapping information
PostPosted: Mon Mar 13, 2006 6:49 am 
Newbie

Joined: Fri Oct 28, 2005 11:33 am
Posts: 13
I would like to access hibernate's mapping information - i.e. what classes are mapped to which tables, their fields and relations, etc. - from within my code. The motivation is to write a custom import module that is supposed to create pojos from imported data.

Specific questions I have would be
    How do I know the class (name) for a given database table?
    How do I know the class (name) for a given XML element (based on hibernate's XML mapping feature)?
    How do I find out about the attributes, their types and column names for a given class?
    How do I find out about the relations to other classes for a given class?


I have browsed the API documentation and it seems Configuration knows about these mappings through PersistentClass. But SessionFactory maintains ClassMetadata which is somewhat different?

I ususally have a reference to the Session only, which gives me access to the SessionFactory as well. But Configuration does not seem to be accessible for me. The documentation also says "A Configuration is intended as a configuration-time object, to be discarded once a SessionFactory is built." -- that means I shouln't access it anyway?

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 3:14 pm 
Newbie

Joined: Tue Jan 25, 2005 3:03 am
Posts: 11
Location: Bangalore, India
Hi,

Quote:
...it seems Configuration knows about these mappings

Yes, you are absolutely right.

Quote:
...Configuration does not seem to be accessible for me.

This is the case if you are using Spring, since Spring internally creates the Configuration object and then calls the buildSessionFactory() method on it to create the SessionFactory.

Quote:
Configuration is intended as a configuration-time object, to be discarded once a SessionFactory is built." -- that means I shouln't access it anyway?

No, it is perfectly valid to use it. What the doc means to say is that the Configuration object is consulted once *and only once* by Hibernate, viz. while building the SessionFactory. In fact, it is not even referenced by any of the Hibernate classes. This is also the problem, as far as your requirement is concerned. You can't get a handle to the configuration through SessionFactory or any other Hibernate class, for that sake.

However, assuming that you are using Spring, there is a way out - the org.springframework.orm.hibernate3.LocalSessionFactoryBean class that you use for configuring Hibernate thru Spring, actually exposes a getter method for the Configuration object.

_________________
Rgds,
Vikas.

"Quidquid latine dictum sit, altum sonatur".
Whatever is said in Latin, sounds profound


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 6:44 am 
Newbie

Joined: Fri Oct 28, 2005 11:33 am
Posts: 13
But if hibernate is consulting the Configuration object only once on startup, it seems to copy the information into its "own" structure - is that the ClassMetadata area? (Seems strange to me or like duplicating information...)

If I use annotations instead of hbm mapping files, will Configuration be the same as with the hbm mapping files?


If I use the Configuration object, that exposes most of the information throuhg PersistedClass pretty nicely. However, I am surprised to find that there is no direct way to get the class (name) from a given XML element name.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Look at the SessionFactory as a compiled version of Configuration, similar to what a .class is for .java.

This also mean that the SessionFactory only contains what is really needed for the ORM to work where as the Configuration have a more highlevel perspective of things.

And yes, Configuration is the base class for AnnotationConfiguration and JDBCConfiguration...

I don't understand the following:

Quote:
However, I am surprised to find that there is no direct way to get the class (name) from a given XML element name.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 11:20 am 
Newbie

Joined: Fri Oct 28, 2005 11:33 am
Posts: 13
For the purpose of hibernate's XML mapping, I can define a (XML) node name for each persisted class, e.g. class x.y.CustomerImpl should use "CUSTOMER" as a node name.
This is available as PersistentClass.getNodeName(). So if I know the class, I know what node name this should be - nice for exporting.
The the other way round is more difficult. There is no way to see what persited class is associated with the "CUSTOMER" node name. Configuration has getClassMapping(classname) for indexed access by class name and getClassMappings() to return an iterator for all. I would need to iterate all persisted classes and check if their getNodeName()=CUSTOMER -- very ineffective.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well, this would need some extra lookup table which we would not need for any thing else but this little case.

similar for "given a column what properties is it mapped too" etc. we can't provide api for every specific query.

but you could just collect the information and cache it..

_________________
Max
Don't forget to rate


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.