0

Can I pass Model as an argument in a method using ActiveJDBC?

Something like this:

public Set<String> getColNames(Model modelName){
    Set<String> set = modelName.attributeNames();
    for(String x: set){
       System.out.prtinln(x);
    }
    return set;
}

That way I can just pass the model and would save a lot of time on doing the same code on each Model right? Like this:

Staff staff = new Staff();
Set<String> set = getColNames(staff);

Is this possible??? Getting the attribute names is just an great example on this, this is not only the purpose why I asked this.

Help would be appreciated!

2 Answers2

0

If you could explain why you need this, it would be easier to give directions.

In any case, you can do this with one line of code;

Map values = model.toMap();

See: Model#toMap()

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Im dealing with several tables and I needed to get all of its attributenames. For example, I have 10 tables. For each table I need to get attributenames, instead of me writing a code (Model.attributeNames) on each table, Id rather create a method that does that and I will just pass the model name on the argument. That way I will only have one method instead of 10. Right? – Vincent Dingding Aug 07 '17 at 15:45
  • why can't you write it like this: `Util#getAttributes(Model ... model)` and internally just write a loop over an array of models to get all the attributes? I still do not understand why you would need it though. – ipolevoy Aug 07 '17 at 16:30
0

I think that what Igor means, is that you can build your own static method, on a class called Util. This method would be something like this.

public static Set<String > getColNames(Model model) {
    return model.toMap().keySet();
}

Then, you can use it like this:

Staff staff = new Staff();
Set<String> set = Util.getColNames(staff);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Martin M.
  • 503
  • 5
  • 10