aspose file tools*
The moose likes Java in General and the fly likes Receive a field from a class as parameter for a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Receive a field from a class as parameter for a method." Watch "Receive a field from a class as parameter for a method." New topic
Author

Receive a field from a class as parameter for a method.

Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Hello there,

I am facing this same situation sometimes, and I wonder if there is any solution...
Usually, when we have a method, it receives value parameters. Instead, I would like to receive field parameter, by this I mean, the indicator of a Field.

Let me give it an example:


This code receive two values, one is Person and the other is the name of this person. Let's suppose Person is a superclass, and it has 2 subclasses: Doctor and Teacher.
Let's also suppose that for some reason, each of these subclasses have a different named Field for keeping the Name of the person. In this case, instead of receiving a copy of the name, I would like to have a reference to it, for instance:


This code doesn't work of course, but is there anything that can be close to it?

My project is to build an automate database manager, not like Hibernate, I do not want the user to write queries, that's why it is needed to me to have а reference to the field of the class received.

Applying my situation to the upper example:

I have a generic method for selects, but how can I tell it where to put the "=", the "!=" and where to consider null values?
I can reflect all of the attributes, and check which is null to disconsider during the select, but what if I have to select "where this field equals null"?


Hope I could make any sense. heheh

Any help will be very welcome.
Thanks in advance.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Micael Carvalho wrote:This code doesn't work of course, but is there anything that can be close to it?

Yes. It's called reflection, and it's horrible.

My project is to build an automate database manager, not like Hibernate, I do not want the user to write queries, that's why it is needed to me to have а reference to the field of the class received.

Ooof. Quite a task. I wouldn't be at all surprised if Hibernate's development cost didn't run to several man years. It also makes a fair bit of use of reflection, from what I gather.

Not to dissuade you, but I'm just saying. If it's only you involved, you might well find out that someone's beaten you to your "better mousetrap" by the time you're done. There are also possibly better choices of language than Java for such an exercise.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Check out how reflection works.

Edit: beaten to it, but Winston is a bit harsh. Reflection can be a very powerful tool, as long as (like any other tool) it isn't abused. Unfortunately, it often is.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18165
    
    8

Micael Carvalho wrote:Let's suppose Person is a superclass, and it has 2 subclasses: Doctor and Teacher. Let's also suppose that for some reason, each of these subclasses have a different named Field for keeping the Name of the person.


But this is already a mistake. Instead of trying to write code which works around it, you would be better off to fix the mistake.
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Winston & Rob,

Actually I am alone, however, this is for personal use only, it doesn't have to contain all the magic functionalities of Hibernate.
I am already using reflection in most of the code. But it has reached a point that either I do not know how to use reflection to go ahead or it just can't go ahead.

I must know what fields to use null values in "where" and what fields not to, there's where I'm stuck... Using reflection I can get all fields and so on, but how can I know which one should I put the "null" in "where" and which I shouldn't? My first idea was to receive a List<String> containg the name for the fields that must consider the null value. Annotation is out of question, since it is not a general rule (sometimes some fields must accept null as a value and other times, they shouldn't).

I haven't applied the List<String> option yet cause I was thinking if there were a better way to do this. I do not want to force the programmer to hardcode the name of the field to call the method. So, I was expecting something like:
Doctor.fields.nameDoctor
This way, I would know that the field that contains the name is called "NameDoctor".

Thank you
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Paul,

It is a mere example, to simplify the understanding. Of course it would be easier and better to fix it. However, as explained in the real example (Database manager), I do not have this option.

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18165
    
    8

A better example would be useful, then. Giving a bad example and asking how to implement it is likely to lead to bad implementations!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Micael Carvalho wrote:I must know what fields to use null values in "where" and what fields not to, there's where I'm stuck... Using reflection I can get all fields and so on, but how can I know which one should I put the "null" in "where" and which I shouldn't?

Surely this is a rule imposed by the database, not Java? In which case you'll have to read data from the meta-tables to work it out. To save time, I suppose you could read this stuff at the time your app is loaded and set up a cooperative "rules" class for each of your DAOs; but to be honest, what you're attempting is beyond my ken.

Winston
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Winston,

Well, I am not quite referring to the null vs not null issue, its already working fine.

My problem is exactly in the select clause. I receive an instance of the class I want to select, containing the parameters like


By using reflection I can check the Annotation on that class that marks the name of the table and the name of each field in the database. I can also get it's value using reflection.

But when there is a null value in any of it's attributes, how to know if I should use "where attrname = null" or just ignore it?
There are cases I must use "attrname = null" (when I want a specific Person that has no name, for instance), and also cases I shouldn't use "attrname = null" (just ignore it), in case I want to select a Person by the date of birth (then, all fields will be null and date of birth will be filled out).

You see?

Annotation can mark fields, but just class fields. I want to mark instance fields, to tell which use should I give to each (equals, not equals, isNull, bigger than, smaller than etc).

Thank you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Micael Carvalho wrote:But when there is a null value in any of it's attributes, how to know if I should use "where attrname = null" or just ignore it?
There are cases I must use "attrname = null" (when I want a specific Person that has no name, for instance), and also cases I shouldn't use "attrname = null" (just ignore it), in case I want to select a Person by the date of birth (then, all fields will be null and date of birth will be filled out).
You see?

Not really, but maybe it's just me being thick. Surely when you do the query, you know what type of query you need to do? Personally, if it was me, I'd just build an "ignore" query from scratch; for a qualified select, I'd use a PreparedStatement.

But I suspect there's something I'm missing.

Winston
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Winston,

PreparedStatement is the class used to run the query... My method is to build this query, after that, I already run it using PreparedStatement.
So, the issue is how to make this method to know which operator to use in each attribute, so it can build the query properly.

Another example:



I have it all done (including the suppresed part). However, my issue lies on building the "whereClauses" variable. I was using equals (=) for every value that is different of null, but now I need to select a Person that has name equals null. My code read the attributes and see that name has null value, therefore, it is ignored on the where clause, since I can't put "where attrName is NULL" for every null field, or I wouldn't be able to select without having all field values (so, what would I be selecting for?).

That is why I am trying to find a way to tell Java which fields should it allow to be used as null value. And it must not be an annotation, because it would be valid for the whole class, and this is just a select case, I could find a Person by it's ID, having the name null and not wanting to have "where name is NULL" on my query.
Passing a list of Strings containg the name of the fields that should allow null values would work, but it is quite a lame solution. I am checking if there is anything better on the table.
This same list would have fixed code, and everything on this library is made to work dynamically (if you change the name of the field in it's Model Class, it will not affect the rest of the system), and doing it would break this dynamism.

Merci

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Micael Carvalho wrote:That is why I am trying to find a way to tell Java which fields should it allow to be used as null value. And it must not be an annotation, because it would be valid for the whole class, and this is just a select case, I could find a Person by it's ID, having the name null and not wanting to have "where name is NULL" on my query.

Which is exactly my point. Experts may correct me, but I'm not sure that a PreparedStatement is the best choice here, because it is a truly dynamic query (ie, the actual statement structure is likely to be different from call to call).

But going back to the business of "nulls"; I still don't see anything to tell me that this isn't a database restriction.

But I say again: I've never tried what you're doing.

Winston
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Winston,

I believe PreparedStatement is always the most recommended option, because it is much more safier than Statement.
My only problem is determining which case is equals (=), not equals (<> or !=) or is null. I can handle PreparedStatement just fine with dynamic queries (a whole structure to insert parameters in the right order and right types is built), so, it's really not related to the issue. :]

It is not a database restriction, I have no errors while running the code, I just need to know "when" to consider the null value as a parameter to my query and "when" not to (sounds like a logic issue, but what I need to know is: Does Java provide any way to pass this extra information to the method?).

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18165
    
    8

Micael Carvalho wrote:I just need to know "when" to consider the null value as a parameter to my query and "when" not to (sounds like a logic issue, but what I need to know is: Does Java provide any way to pass this extra information to the method?).


Well, of course it does. Once you know "when", then you have a boolean value (true or false) which you can pass via a suitably-defined parameter. But surely that wasn't your question? I suspect you really wanted to know the first part, where you started out "I just need to know...", which seems much less trivial.
Micael Carvalho
Greenhorn

Joined: Jul 10, 2012
Posts: 9

Paul,

Yes, quite confusing sentense, sorry.
Well, I am trying to find an optimal way to do this. Passing a boolean would definitely work, but it would not be nice, since there would by many parameters (one for each special case [null, equals, not equals, bigger than, smaller than etc). To be more specific, I am looking for some kind of Annotation that is instance based and not class based. The commom Annotation will work fine for the whole class, but can I easily change specific Annotated fields for only one instance?

Not working example:


This code would select every register on the person table that have the field name with the null value. (SELECT * FROM person WHERE name IS NULL)

You really helped me to explain what my problem is (I was having trouble on it haha)! Tks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Receive a field from a class as parameter for a method.
 
Similar Threads
Passing object reference variables
Help with writing a remove() method
Rule of thumb for designing data classes
lists inside a list :(((
inheritance