join - Using a Spring RowMapper in order to populate a field of type Collection from a JDBC resultset -


i trying map jdbc resultset pojo using spring rowmapper follows:

public class advertisementmapper implements rowmapper<advertisement> {      @override     public advertisement maprow(resultset rs, int rownum) throws sqlexception {         advertisement advertisement = new advertisement();         advertisement.setid(rs.getlong("id"));         advertisement.setadvertisementtype(advertisementtype.valueof(rs.getstring("advertisement_type")));         advertisement.setneed(need.valueof(rs.getstring("need")));         advertisement.setchildcareworkertype(childcareworkertype.valueof(rs.getstring("childcare_worker_type")));         advertisement.setchildcaretypes(null);//collection!         advertisement.setdaytotimeslots(null);//collection!         advertisement.setlanguages(null);//collection!         advertisement.setdescription(rs.getstring("description"));         advertisement.setcreationdate(rs.getdate("creation_date"));         advertisement.setexpirationdate(rs.getdate("expiration_date"));         advertisement.setactive(rs.getboolean("active"));         advertisement.setvalidated(rs.getboolean("validated"));         member member = new member();         member.setid(rs.getlong("memberid"));         advertisement.setmember(member);         address address = new address();         address.setlat(rs.getdouble("addresslat"));         address.setlon(rs.getdouble("addresslon"));         advertisement.setaddress(address);         advertisement.setfirstaidtraining(choice.valueof(rs.getstring("first_aid_training")));         advertisement.setgivebath(choice.valueof(rs.getstring("give_bath")));         advertisement.setpreparemeals(choice.valueof(rs.getstring("prepare_meals")));         advertisement.sethelpwithhomework(choice.valueof(rs.getstring("help_with_homework")));         advertisement.setcleaningironing(choice.valueof(rs.getstring("cleaning_ironing")));         advertisement.setversion(rs.getint("version"));         return advertisement;     } } 

i have issue 3 fields of type: java.util.set (i have added //collection! comment them in java code above.)

here sql:

select *  advertisement a,  advertisement_childcare_types act,  advertisement_day_to_time_slots adtts,  day_to_time_slot dtts, advertisement_languages al, language l  a.id = act.advertisement , a.id = adtts.advertisement , adtts.day_to_time_slots = dtts.id , a.id = al.advertisement , al.languages = l.id 

i not sure how populate collection/java.util.set fields resultset...

can please help?

since don't have one-to-one mapping rows in resultset advertisement instances, won't able rowmapper. need use rowcallbackhandler or resultsetextractor<list<advertisement>>. advantage rowcallbackhandler don't need deal calling next().

here non-threadsafe, not super-efficient, clear implementation:

class advertisementrowcallbackhandler implements rowcallbackhandler {    map<long, advertisement> results = new hashmap<>();    @override   void processrow(resultset rs) sqlexception {     long id = rs.getlong("id");     advertisement advertisement = new advertisement();     advertisement.setchildcareworkertypes(new hashset<>());     results.putifabsent(id, advertisement);     advertisement = results.get(id);     // smart setting these if absent in map     advertisement.setid(id);     advertisement.setdescription(rs.getstring("description"));     advertisement.getchildcareworkertypes().add(rs.getstring("worker_type"));     // ...   } } 

and result new arraylist<>(handler.results.values()).


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -