In Java - How to map resultSet into a complex object?

前端 未结 4 1246
生来不讨喜
生来不讨喜 2021-01-24 02:12

How can I map a resultset from a few tables into a complex object? let me elaborate:

Lets say I have these 2 classes:

public class User {
    private int         


        
4条回答
  •  [愿得一人]
    2021-01-24 02:48

    Create a HashMap of where user id is the key, and user is the value. Then when you process the result set, do map.get(resultset.get(userid)), if it's null, create a new user, but if it isn't then do the logic to append a user to the country.

    HashMap users;
    HashMap countries;
    //inside the result set processing
    User u = users.get(resultSet.get("id"));
        if(null != u) {
          //populate user here
        } else {
           Country c = countries.get(resultSet.get(country);
           c.users.add(u);
        }
    

提交回复
热议问题