views:

86

answers:

2

If there's already a question that addresses this, then could I please get a link as I cannot find one.

I'm looking to obtain the stored class name of an object stored in my Hibernate database. When I look at the database externally I see the strings stored that have the classname. How can I retrieve the class name without constructing the object?

Thank you in advance.

Edit: No I am not specifically specifying the discriminator; these are in fact subclasses being stored. I'm simply trying to get the actual subclass of the object.

+1  A: 

There are several ways, depending on how your instances are mapped into the database format. In both cases, the first step is to get hold of the Configuration instance of Hibernate which contains the mapping config (see the API docs).

The root object is Configuration, the information you seek is probably returned by getTableMappings(). You'll have to use a debugger and the Hibernate source and some time to figure out how everything works.

If you're using Spring, you'll have to figure a way to get at the Configuration object. Your best bet is to set a breakpoint in Configuration.buildSessionFactory(). That should give you an idea.

Aaron Digulla
I will certainly look into this, thank you.
AlbertoPL
http://stackoverflow.com/questions/516906 - that answer shows you how to loop through all the mappings. PersistentClass.getTable will get you the table name so if you're starting with the table you should be able to loop through all the class mappings until you find the one you want. Subclasses may confuse the issue since one table can map to multiple classes.
Brian Deterling
@Brian: You should post this as an answer :)
Aaron Digulla
A: 

I created a new field in the object that stored its type. This is what I used for my solution, however I'd still like answers as to how to access the discriminator value.

AlbertoPL