-->
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: how to avoid typing explicit field path in HQL queries?
PostPosted: Fri Apr 20, 2007 6:14 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 6:54 am
Posts: 22
Hi,
when I write HQL queries, I always have to type the property names explicitly, something like

from Person person where person.age = 30

if I want to change the name of the age property, I have to change it in the HQL string as well. (Actually when I create HQL strings I use some home made dictionary for getting the property names, but I still have to maintain the dictionary.)

So my question is, is it possible through reflection to get the path name to some property? So that I could do

<Some nice reflectionFramework>.getPathOfProperty(person.getAge())

?


Using hbn 3.2 and annotations.

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 9:11 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
That is one confusing question :D

You want to pass the getter method and see what field is backed by that method? I can't even imagine a way to do that. Or is person.getAge() supposed to return somethign else?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 10:13 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 6:54 am
Posts: 22
Thanks, you understand my question, even though it was quite unclear I agree :)

To be precise, what I want is not the backing field, but the property, isnt Hibernate referencing the property name rather than the backing field in HQL?

So what I basically want is a function that gives me the answer to this question:
"what is the path of the property that is represented by this getter?"

By doing:

Person person = new Person();
String thePath = theFunctionIwant( person.getAge() )

thePath would contain "Person.age". (I know it is impossible this way, since the getAge() would return an int or something, so what I really need is a way to reference that this is a property...)

Then I could construct my HQL without having to duplicate the information that age is a property of Person through some private dictionary.

Or am I missing out on some basic concept in Hibernate here? If not, I would believe that other people would be bothered by the need for duplicating information as well?

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 1:13 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I suppose that you can lurk through the Configuration metadata, but I've never done that myself. It certainly sounds like you would benefit more from building Critieria queries, since they are a more object oriented way to build queries, especially dynamically generated ones.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 1:16 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Now that I've reread your post, since you are using annotations, you can use reflection to get the annotation metadata. Again, I've no experience with that, but I've seen several exaamples on the web.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 3:36 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 6:54 am
Posts: 22
Thanks for the input! I have tried using the Criteria API, more preciescely the "Example" class, but it turns out that for some reason you cannot query PK through that.
I will have a look at what is possible through annotations + reflection then.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 5:20 am 
Newbie

Joined: Tue Oct 24, 2006 5:24 am
Posts: 2
I think that what you want is currently impossible. If I understand it correctly you would like the equivalent of the .class notition but then for methods: a method reference. This does not currently exist in Java (sadly enough) and is part of the debate for Java 7.
Related to this is the Java 7 discussion for first-class "properties"; property references would fill this need too.

Many people have the same problem you see: having Java properties or methods referred to within strings hides them from refactoring and compiler error checking. Most frameworks have problems with this but sadly enough it looks like it will take quite some time before Java aquires this ability..


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.