Update Java object with a Hashmap -


i have model properties e.g.

public class example { long id; string a, b; int c, d; boolean e; } 

now want create method this

public void update(long id, map<string, object> properties) { .... } 

in properties map want have sth like

properties.put("a","test"); properties.put("c", 8);

i'm not sure on how achieve this.

at end want sth this:

example e = new example(....); ..... e.update(5l,properties); 

can some1 point me correct path? cant figure out searchterm doesnt lead me properties or hashmap entries.

thanks in advance

you searching keyword reflection. reflective access write update method that:

public void update(long id, map<string, object> properties) {     object obj = getobjectbyid(id); // have implement method     (string property : properties.keyset()) {         field field = obj.getclass().getfield(property);         field.set(obj, properties.get(property));     } } 

note, did not declare or handle exceptions come along reflection.


a other issue: why want way? using reflection update fields of object smells real design issue. should consider model.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -