-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting class-level metadata
PostPosted: Mon Jan 30, 2006 9:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Can I get at the <meta> elements of a mapping? The normal elements are accessible via SessionFactory.getClassMetadata, but I can't see how to get to the meta elements.

Alternatively, is there some way to mark a property in a mapping file so that I can get its name in java code?

I'll explain what I'm trying to do, in case anyone can point out a better way. Many of the objects in my app have a "point-of-contact" member, which is the higher-level object that is responsible for that object. For example, Book has getAuthor, Pet has getOwner, Car has getRegisteredOwner, Child has getLegalGuardian, etc. How do I get the name of the property (or specifically, the many-to-one mapping) of those point-of-contact members, so that I can dynamically build an HQL query to get it from the object that I start with?

The only solution I've come up with so far is to put the mapping name as a class member of the "owned" object, but I don't want to put hibernate details in my POJO code if there's an alternative.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:14 am 
Newbie

Joined: Fri Nov 25, 2005 10:01 pm
Posts: 7
Can you use plain old java reflection - Pet.class.getDeclaredMethods()
and then figure out which get is the one you want?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 1:56 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Configuration.getClassMapping("Your persistentClass").getProperty("Property name").getMetaAttribute("attribute name").getValue()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 5:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think that using reflection and heuristics would be undesirable. Not only is it mixing up business logic and data access code, it's also extremely slow compared to almost any alternative.

The Configuration.* route looks good, but SessionFactories don't maintain a link to their Configurations; I'd have to store the Configuration somewhere for this purpose, and search through it every time I wanted one of these points of contact. Even caching the reference in a class variable would probably end up having a performance hit, because all of this is in a webapp so there's new classloaders involved for every session. However, I think I'll be able to make other uses of getClassMappings(), now that you've pointed it out to me.

I think I'm going to have to store the reference in a class constant. It's inelegant but efficient and correct.

Thanks for the ideas, guys.


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