aspose file tools*
The moose likes Java in General and the fly likes Finding attributes in an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding attributes in an object" Watch "Finding attributes in an object" New topic
Author

Finding attributes in an object

Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
I have an object called SearchCriteria with about 20 attributes, each one being a criteria via which one could search.




I need to build a db query based on any of these criteria. I'd like to programmatically loop through the object's attributes testing each one for null and then taking action on the ones that are not null. How do you programmatically loop through an object's attributes?

TDR
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You could do this with reflection.
But I think you could come up with a better solution. Hibernate has a criteria class. You could look at that.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
I can definitely do this using hibernate restrictions, but the difficulty is that these criteria come from up to 7 tables and in order to be able to use the restrictions I need to fetch join all 7 tables. I'm only displaying data from 2 of these tables so why do the huge fetch? Big waste of resources.

So I'm trying to come up with a different way of finding the objects.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding attributes in an object